6

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.

Biscuit128
  • 5,026
  • 21
  • 84
  • 142
  • 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 Answers2

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
  • 7,362
  • 3
  • 34
  • 52
1

See Mockito API Article 7. Making sure interaction(s) never happened on mock

Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266