我对Mockito还是很陌生,尽管我之前已经成功地运行了测试,但今天看起来真是太奇怪了,为什么mockito verify()方法一直告诉我需要一个View
方法,即使我确定Presenter
也没有被调用方法正确调用它。这是我的测试代码:
@Test
public void loadCallPlans_shouldUpdateAdapter() {
ArrayResponse<CallPlan> callPlans = new ArrayResponse<>();
callPlans.data = Arrays.asList(new CallPlan[]{new CallPlan(), new CallPlan()});
Single response = Single.just(callPlans);
when(callPlanRepository.getCallPlans()).thenReturn(response);
presenter.loadCallPlans("Today");
doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
System.out.println("View.updateCallPlans() called with arguments: " + java.util.Arrays.toString(args));
return null;
}).when(view).updateCallPlans(anyList());
verify(view).updateCallPlans(anyList());
}
我可以确认调用了
updateCallPlans()
方法,因为我在logcat中得到了View.updateCallPlans() called with arguments: [[]]
。我完整的日志:View.updateCallPlans() called with arguments: [[]]
Wanted but not invoked:
view.updateCallPlans(<any List>);
-> at CallPlansPresenterTest.loadCallPlans_should UpdateAdapter(CallPlansPresenterTest.java:69)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
view.updateCallPlans(<any List>);
-> at CallPlansPresenterTest.loadCallPlans_should UpdateAdapter(CallPlansPresenterTest.java:69)
Actually, there were zero interactions with this mock.
at CallPlansPresenterTest.loadCallPlans_shouldUpdateAdapter(CallPlansPresenterTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
如果我使RxJava返回错误作为响应,则测试运行良好并且
View.setError()
已正确验证: @Test
public void loadCallPlans_shouldShowError() {
String error = "Error";
Single response = Single.error(new Throwable(error));
when(callPlanRepository.getCallPlans()).thenReturn(response);
presenter.loadCallPlans("Today");
doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
System.out.println("View.setError() called with arguments: " + java.util.Arrays.toString(args));
return null;
}).when(view).setError(anyString());
verify(view).setError(error);
}
最佳答案
您实际上在嘲笑对updateCallPlans的调用
doAnswer((Answer<Void>) invocation -> {
Object[] args = invocation.getArguments();
System.out.println("View.updateCallPlans() called with arguments: " + java.util.Arrays.toString(args));
return null;
}).when(view).updateCallPlans(anyList());
}
因此它将永远不会调用实际方法。
您的验证应在答案调用内。