verifyNoMoreInteractions

verifyNoMoreInteractions

当我排除 verifyNoMoreInteractions(daoInteractor) 行时,我的测试将通过。我认为将 verifyNoMoreInteractions() 放在测试结束时就像我所做的那样意味着不会发生这种失败?

或者,如果我排除 assertEquals() 行并保留 verifyNoMoreInteractions() ,它将通过。

我测试过度了吗?

when(daoInteractor.getViewedReleases()).thenReturn(viewedReleases);

verify(daoInteractor, times(1)).getViewedReleases();
line 210: assertEquals(daoInteractor.getViewedReleases(), viewedReleases);

// line 216: verifyNoMoreInteractions(daoInteractor);

堆栈跟踪:
org.mockito.exceptions.verification.NoInteractionsWanted:
No interactions wanted here:
-> at bj.main.MainPresenterUnitTest.buildRecommendationsError_ControllerError(MainPresenterUnitTest.java:216)
But found this interaction on mock 'daoInteractor':
-> at bj.main.MainPresenterUnitTest.buildRecommendationsError_ControllerError(MainPresenterUnitTest.java:210)
***
For your reference, here is the list of all invocations ([?] - means unverified).
1. -> at bj.main.MainPresenter.buildRecommendations(MainPresenter.java:160)
2. [?]-> at bj.main.MainPresenterUnitTest.buildRecommendationsError_ControllerError(MainPresenterUnitTest.java:210)


    at bj.main.MainPresenterUnitTest.buildRecommendationsError_ControllerError(MainPresenterUnitTest.java:216)
    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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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.AppMain.main(AppMain.java:147)


Process finished with exit code -1

最佳答案

问题不在于 verifyNoMoreInteractions(daoInteractor);
在这一行,您期待方法调用:

verify(daoInteractor, times(1)).getViewedReleases();

然而,电话还没有发生。您不是在告诉 mockito 您希望调用在 future 发生,而是在验证调用是否已经发生。

解决方法是在调用后移动该行:
assertEquals(daoInteractor.getViewedReleases(), viewedReleases);
verify(daoInteractor, times(1)).getViewedReleases();
verifyNoMoreInteractions(daoInteractor);

关于android - Mockito verifyNoMoreInteractions() 失败测试,​​否则在使用 assertEquals() 时通过,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43844091/

10-13 04:30