Mockito verifyNoMoreInteractions() failing test that passes otherwise when using assertEquals()
android, junit, mockito
Solution
The problem is not with `verifyNoMoreInteractions(daoInteractor);`
At this line you are expecting method call:
verify(daoInteractor, times(1)).getViewedReleases();
However the call didn't happen yet. You are not telling mockito that you expect call to be happen in the future, you are verifying that the call already occurred.
The solution is to move the line after the call:
assertEquals(daoInteractor.getViewedReleases(), viewedReleases);
verify(daoInteractor, times(1)).getViewedReleases();
verifyNoMoreInteractions(daoInteractor);
Problem
My test will pass when I exclude the `verifyNoMoreInteractions(daoInteractor)` line. I thought placing the `verifyNoMoreInteractions()` at the end of the test as I have done would mean this failure wouldn't happen? Alternatively, it will pass if I exclude the `assertEquals()` line and keep `verifyNoMoreInteractions()`. Am I over testing? ``` when(daoInteractor.getViewedReleases()).thenReturn(viewedReleases); verify(daoInteractor, times(1)).getViewedReleases(); line 210: assertEquals(daoInteractor.getViewedReleases(), viewedReleases); // line 216: verifyNoMoreInteractions(daoInteractor); ``` Stacktrace: ``` 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 ```