Mockito:验证内部匿名类的方法调用 [英] Mockito: verifying method call from internal anonymous class
问题描述
我有一个正在测试的类,其中包含一个具有内部匿名类的方法.匿名类中的一种方法从被测类中调用一种方法,但是Mockito似乎没有意识到这一点.
I have a class under test which contains a method which has an inner anonymous class. One of the methods in the anonymous class calls a method from the class under test, but Mockito doesn't seem to realize this.
public class ClassUnderTest {
Dependency dependency;
public ClassUnderTest(Dependency d) {
dependency = d;
}
public void method() {
dependency.returnsObservable().observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io()).subscribe(new Observer<SupportClass> {
/* Other methods omitted */
public void onComplete() {
outerMethod();
})
}
public void outerMethod() {
blah;
}
}
我的测试代码:
public class TestClass {
ClassUnderTest underTest;
Dependency dependency;
@Before
public void setUp() throws Exception {
dependency = Mockito.mock(Dependency.class);
underTest = Mockito.spy(new ClassUnderTest(dependency));
}
@Test
public void method() throws
Mockito.when(dependency.returnObservable()).thenReturn(Observable.just(new SupportClass());
Mockito.doNothing().when(underTest).outerMethod();
underTest.method();
Mockito.verify(underTest).outerMethod();
}
}
由于某种原因,我似乎无法弄清楚,即使我已通过在调试器中逐行逐步进行验证,Mockito也无法检测到正在调用outsideMethod().我还验证了对依赖对象的调用返回了带有正确内容的正确的Observable,并且确实调用了onComplete()和outsideMethod()方法.我只是感到困惑,为什么Mockito不能这样检测到它.
For some reason that I can't seem to figure out, Mockito can't detect that outerMethod() is being called, even though I have manually verified by stepping through line by line in the debugger. I have also verified that the call to the dependency object returns the proper observable with the correct content, and the onComplete() and outerMethod() methods do get called. I'm just confused why Mockito doesn't detect it as such.
这是它吐出的错误:
Wanted but not invoked:
classUnderTest.outerMethod();
-> at (file and line number)
However, there was exactly 1 interaction with this mock:
classUnderTest.method();
-> at (file and line number)
有什么明显的我想念的吗?
Is there anything obvious I'm missing?
推荐答案
您在调度程序之间进行了更改,因此在测试时可能会引起一些问题(您的代码可能在之前到达了verify
方法实际方法被调用
You're changing between schedulers so it can cause some issues when testing (your code may reach the verify
method before the actual method is invoked
查看本文解释如何使用RxJava和Mockito测试异步代码
Check this article explaining how to test asynchronous code with RxJava and Mockito
TL; DR
添加将所有调度程序设置为trampoline
的TestRule
,以便其同步运行:
Add a TestRule
that set all schedulers to trampoline
so it behaves synchronously:
public class TrampolineSchedulerRule implements TestRule {
@Override
public Statement apply(final Statement base, Description d) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(
scheduler -> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(
scheduler -> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(
scheduler -> Schedulers.trampoline());
RxAndroidPlugins.setInitMainThreadSchedulerHandler(
scheduler -> Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
RxAndroidPlugins.reset();
}
}
};
}
}
这篇关于Mockito:验证内部匿名类的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!