3.6 调用者的上下文

作者: 孙兴斌 | 来源:发表于2016-12-30 11:40 被阅读29次

在Fake类的方法中,可以设置一个Invocation类对象,在该方法被调用时,JMockit会为这个对象传值。这种机制的意义是Fake类可以接触到真实类。因此,Fake类的方法可以得到调用参数,调用次数等信息。


@Test
public void invocation() throws Exception {

   final Subject testSubject = new Subject();

   new MockUp<LoginContext>() {
      @Mock
      void $init(Invocation invocation, String name, Subject subject)
      {
         assertNotNull(name);
         assertSame(testSubject, subject);

         // Gets the invoked instance.
         LoginContext loginContext = invocation.getInvokedInstance();

         // Verifies that this is the first invocation.
         assertEquals(1, invocation.getInvocationCount());
      }

      @Mock
      void login(Invocation invocation)
      {
         // Gets the invoked instance.
         LoginContext loginContext = invocation.getInvokedInstance();

         // getSubject() returns null until the subject is authenticated.
         assertNull(loginContext.getSubject());
      }

      @Mock
      void logout(Invocation invocation)
      {
         // Gets the invoked instance.
         LoginContext loginContext = invocation.getInvokedInstance();

         assertSame(testSubject, loginContext.getSubject());
      }
   };

   LoginContext theFakedInstance = new LoginContext("test", testSubject);
   theFakedInstance.login();
   theFakedInstance.logout();
}

相关文章

  • 3.6 调用者的上下文

    在Fake类的方法中,可以设置一个Invocation类对象,在该方法被调用时,JMockit会为这个对象传值。这...

  • -普通函数this和箭头函数this

    普通函数中的this: this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ,...

  • es6中箭头函数this指向(2)

    普通函数中的this: this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ,...

  • javascript之this探究

    this是基于上下文环境来变动的 可以用不同的方法进行函数调用,不用的调用机制决定了函数上下文的不同。调用者 ca...

  • this

    在一个函数上下文中,this由调用者提供,由调用函数的方式来决定。如果调用者函数,被某一个对象所拥有,那么该函数在...

  • js中匿名函数、箭头函数

    匿名函数 匿名函数的this指向调用者改变this指向的方法有 call():第一个参数为函数上下文的对象,后面为...

  • CFG转CNF(附代码)

    定理3.6 任一上下下文无关文法都可以用乔姆斯基范式的上下文无关文法产生。 证明思路 能够把任一上下文无关文法G转...

  • JS中的this

    JS中的this 众所周知,JS中this的代表的是当前函数调用者的上下文。JS是解释性的动态类型语言,函数在调用...

  • 浅谈js中的this

    js中的this大家都有基本的共识,那就是执行上下文,当前调用者。下面讲述一个简单的例子 对于第一个打印是obj没...

  • 同步异步/阻塞非阻塞

    同步异步 同步和异步体现的是消息通知的方式 同步:调用者调用被调用者,被调用者在返回其结果前,调用者不会返回。实际...

网友评论

    本文标题:3.6 调用者的上下文

    本文链接:https://www.haomeiwen.com/subject/sjrmvttx.html