context.checking(new Expectations() {{
allowing(appDA).getAppbyApiKey("invalidKey"); will(returnValue(null));
allowing(appDA).getAppbyApiKey("testValidKey"); will(returnValue(app));
allowing(accountDA).insertFrob(with(any(Frob.class)));
}});
请帮我讲解一下上面的语法,最好能讲透一点.多谢.

解决方案 »

  1.   

    context.checking(a)//a可能是整数、字符串、数组等等
    a = new Expectations(){b}//此时a为一个对象(new Expectations()),它的实现是b
    b = {allowing(appDA).getAppbyApiKey("invalidKey"); will(returnValue(null));…}//b包含了一些方法什么什么的
      

  2.   

    new Expectations() {}
    -----------
    这是匿名类Anonymous Class的写法,Expectations是一个接口或者纯虚类或者普通类,就其在代码的定义来看,应该不是接口而是类。{}里面的{}
    是对象初始化代码,先于构造方法执行这种写法类似于:Class AnonymousExpectations extends Expectations{
        AnonymousExpectations(){
        }
        //这里的代码先于AnonymousExpectations 构造方法执行
        {
            allowing(appDA).getAppbyApiKey("invalidKey"); 
            will(returnValue(null));
            allowing(appDA).getAppbyApiKey("testValidKey"); 
            will(returnValue(app));
            allowing(accountDA).insertFrob(with(any(Frob.class)));
        }

    ............
    context.checking(new AnonymousExpectations());
      

  3.   


    首先
    这是一个匿名内部类,
    extends Expectations
    而这个内层的{}号是一个匿名的构造器,{
    allowing(appDA).getAppbyApiKey("invalidKey"); will(returnValue(null));
    allowing(appDA).getAppbyApiKey("testValidKey"); will(returnValue(app));
    allowing(accountDA).insertFrob(with(any(Frob.class)));
    }这个构造器不能使用super();和this();(包括有参数的)外层{}是类的定义区,和其它的类编码没什么区别