How to verify that a specific method was not called using Mockito?
Even more meaningful :
import static org.mockito.Mockito.never;import static org.mockito.Mockito.verify;// ...verify(dependency, never()).someMethod();
The documentation of this feature is there ยง4 "Verifying exact number of invocations / at least x / never", and the never
javadoc is here.
As a more general pattern to follow, I tend to use an @After
block in the test:
@Afterpublic void after() { verifyNoMoreInteractions(<your mock1>, <your mock2>...);}
Then the test is free to verify only what should be called.
Also, I found that I often forgot to check for "no interactions", only to later discover that things were being called that shouldn't have been.
So I find this pattern useful for catching all unexpected calls that haven't specifically been verified.