How can you verify a mocked object is not invoked at all? I am trying to test the empty implementation of an interface method using Mockito.
Asked
Active
Viewed 2,779 times
7
-
Have you tried anything so far? – Dylan Corriveau Mar 03 '15 at 13:48
-
Yes but the only approaches I know of (using mockito with verify) are on a per method basis - not a blanket for all methods statement – Biscuit128 Mar 03 '15 at 13:52
-
possible duplicate of [How to verify that a specific method was not called using Mockito?](http://stackoverflow.com/questions/12862659/how-to-verify-that-a-specific-method-was-not-called-using-mockito) – SpaceTrucker Mar 03 '15 at 13:53
2 Answers
6
I use org.mockito.Mockito.verifyNoMoreInteractions
.
In fact, personally, I always include this section in all my Mockito tests:
@After
public void after() {
verifyNoMoreInteractions(<your mock1>, <your mock2>...);
}
So it acts as a handy catch-all to ensure that the test has no left-over, unexpected invocations that I haven't specifically verified.
I find that more useful than cluttering the tests with specific verifyZeroInteractions
.

David Lavender
- 8,021
- 3
- 35
- 55
3
See Mockito API Article 7. Making sure interaction(s) never happened on mock

Evgeniy Dorofeev
- 133,369
- 30
- 199
- 275