Mockito调用侦听器确定源



我有一个简单的接口,并用InvocationListener模拟它(通过详细的日志记录选项模拟)。

final SampleInterface sampleIf = mock(SampleInterface.class, withSettings().verboseLogging());
System.out.println("-> Modify return value");
when(sampleIf.getIt()).thenReturn("Result");
System.out.println("-> Call getter");
assertEquals("Result", sampleIf.getIt());
System.out.println("-> Verify getter call");
verify(sampleIf).getIt();

结果我得到以下输出:

->修改返回值

######mock/spy上的日志方法调用#1#######sampleInterface.getIt();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:31)
已返回:"null">

->呼叫吸气剂

######mock/spy上的日志方法调用#2#######stubed:->atde.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:31)sampleInterface.getIt();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:33)
已返回:"Result"(java.lang.String)

->验证getter调用

######mock/spy上的日志方法调用#3#######sampleInterface.getIt();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:35)
已返回:"null">

我的问题是,如何将真正的getter调用与mockito框架所做的调用隔离开来?

我喜欢有以下输出:

->修改返回值->呼叫吸气剂

######mock/spy上的日志方法调用#2#######stubed:->atde.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:31)sampleInterface.getIt();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:33)
已返回:"Result"(java.lang.String)

->验证getter调用

编辑-没有mock的示例

System.out.println("-> Calling anything not mocked");
sampleIf.callMe();
System.out.println("-> Verify call");
verify(sampleIf).callMe();

->调用任何未被嘲笑的

######mock/spy上的日志方法调用#4#######sampleInterface.callMe();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:38)
已返回:"null">

->验证呼叫

######mock/spy上的日志方法调用#5#######sampleInterface.callMe();调用时间:->de.sambalmueslie.samanunga.test.TesterTest.test(TesterTest.java:40)
已返回:"null">

从您的问题陈述中,我相信您希望从测试中的代码中收到对存根的调用通知,并在制定对模拟对象的期望时排除从测试代码中生成的调用。

使用InvocationListener确实可以实现这一点。我在下面写了一个监听器,它过滤传递给示例中使用的详细调用记录器的通知。

final InvocationListener listener = new InvocationListener() {
final VerboseMockInvocationLogger logger = new VerboseMockInvocationLogger();         
@Override
public void reportInvocation(MethodInvocationReport methodInvocationReport) {
if (methodInvocationReport.getLocationOfStubbing() != null) {
logger.reportInvocation(methodInvocationReport);                  
}
}
};
MockSettings settings = Mockito.withSettings().invocationListeners(listener); 
SampleInterface sampleIf = Mockito.mock(SampleInterface.class, settings);
...

产生输出:

-> Modify return value
-> Call getter
############ Logging method invocation #1 on mock/spy ########
stubbed: -> at a.a.A.a(A.java:29)
sampleInterface.getIt();
invoked: -> at a.a.A.a(A.java:31)
has returned: "Result" (java.lang.String)
-> Verify getter call

我用Mockito 1.10.19来测试这个。

编辑

好的,我理解了-您还希望收到未被嵌入方法调用的通知。在这种情况下,我确实认为仅使用InvocationListener是不够的。因此,我不知道有什么简单的方法可以通过Mockito提供的框架将"真实"方法调用(存根或未存根)与期望的定义区分开来。

如果您事先知道哪些方法是存根的,哪些不是,那么您可以为未存根的方法调用使用默认的Answer。当您创建mock(Mockito.mock(type, defaultAnswer))时,可以提供一个默认答案。它将仅对未缓冲的方法调用进行调用。除了InvocationListener之外,它还可以用于收集mock上的方法调用。

然而,即使我们可以将我们知道被存根的方法调用与不被存根的调用分离开来,我怀疑我们是否愿意将逻辑分离为两个不同的结构,我也不建议这样做,因为它似乎不一致,而且对我来说很容易出错

相关内容