Biscuit128
Biscuit128

Reputation: 5398

Mockito - verify object not invoked at all

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.

Upvotes: 7

Views: 2974

Answers (2)

David Lavender
David Lavender

Reputation: 8301

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.

Upvotes: 6

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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

Upvotes: 3

Related Questions