Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - verify object not invoked at all

Tags:

java

mockito

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.

like image 910
Biscuit128 Avatar asked Mar 03 '15 13:03

Biscuit128


2 Answers

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.

like image 50
David Lavender Avatar answered Sep 28 '22 18:09

David Lavender


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

like image 25
Evgeniy Dorofeev Avatar answered Sep 28 '22 17:09

Evgeniy Dorofeev