Mockito spy - when calling inner class method not spying method in spy object(Mockito spy - 当调用内部类方法而不是间谍对象中的间谍方法时)
问题描述
我有如下内部类的类:
public class ClassWithInnerObject {
private final InnerObject innerObject;
public ClassWithInnerObject() {
innerObject = new InnerObject();
}
public void callInnerObjectMethod() {
innerObject.outerFunc();
}
public void outerFunc() {
innerFunc();
}
public void innerFunc() {
Log.d("XXX", "innerFunc: called");
}
public class InnerObject {
public void outerFunc() {
innerFunc();
}
}
}
mockito 测试如下所示:build.gradle:
And the mockito test is looking as following: build.gradle:
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
测试:
@RunWith(AndroidJUnit4.class) public class SpyVerifyTest {
@Test public void myInnerTestWorking() {
ClassWithInnerObject p = new ClassWithInnerObject();
ClassWithInnerObject spy = Mockito.spy(p);
spy.outerFunc();
verify(spy, times(1)).innerFunc();
}
@Test public void myInnerTestNotWorking() {
ClassWithInnerObject p = new ClassWithInnerObject();
ClassWithInnerObject spy = Mockito.spy(p);
spy.callInnerObjectMethod();
verify(spy, times(1)).innerFunc();
}
}
第一个测试按预期工作.第二个 innerFunc
从未被检测为已调用",尽管在日志中我看到它是.出了什么问题?:)
The first test is working as expected.
The second one the innerFunc
is never detected as "invoked", although in the log I see it is.What is wrong? :)
谢谢!
推荐答案
怎么了?
好吧,这里的问题很微妙,当你调用 Mockito.spy(p)
时,mockito
在你的 ClassWithInnerObject
允许监视实例上的所有方法调用.多亏了这一点,您可以检查给定方法被调用了多少次但仅在装饰器上而不是在您的实例上.在这里,当您使用内部类时,它会在您的 ClassWithInnerObject
实例上调用 innerFunc()
而不是在装饰器上调用 Mockito
innerFunc()
没有被调用.
Well, the problem here is quite subtle, when you call Mockito.spy(p)
, mockito
creates behind the scene some kind of decorator over your instance of ClassWithInnerObject
allowing to monitor all methods calls on your instance. Thanks to that, you can check how many times a given method has been called but on the decorator only not on your instance. And here, when you use an inner class, it calls innerFunc()
on your instance of ClassWithInnerObject
not on the decorator so for Mockito
innerFunc()
has not been called.
这篇关于Mockito spy - 当调用内部类方法而不是间谍对象中的间谍方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mockito spy - 当调用内部类方法而不是间谍对象中的间谍方法时


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01