Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito verify no more interactions with any mock

Tags:

java

mockito

In Mockito, is there a way to verify that there are no more interactions on any mock I have created?

For example:

public void test() {   ...   TestObject obj = mock(TestObject);   myClass.test();   verifyNoMoreInteractionsWithMocks();  <------- } 

Is there such a method?

like image 341
sworded Avatar asked Aug 17 '12 21:08

sworded


People also ask

Which method in Mockito verifies that no interaction has happened with a mock in Java?

Mockito verifyZeroInteractions() method It verifies that no interaction has occurred on the given mocks. It also detects the invocations that have occurred before the test method, for example, in setup(), @Before method or the constructor.

What does verify no more interactions do?

verifyNoMoreInteractions() Checks if any of given mocks has any unverified interaction. We can use this method after calling verify() methods. It is to make sure that no interaction is left for verification.

What does verify method do in Mockito?

Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.


1 Answers

Since verifyNoMoreInteractions take an array of object we have to find a way to get all the created mocks.

You can create this class

public class MocksCollector {     private final List<Object> createdMocks;      public MocksCollector() {         createdMocks = new LinkedList<Object>();         final MockingProgress progress = new ThreadSafeMockingProgress();         progress.setListener(new CollectCreatedMocks(createdMocks));     }      public Object[] getMocks() {         return createdMocks.toArray();     } } 

and then use it in your test :

    public class ATest {     private final MocksCollector mocksCollector = new MocksCollector();      @Test     public void test() throws Exception {         A a1 = mock(A.class);         A a2 = mock(A.class);         A a3 = mock(A.class);         assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);         verifyNoMoreInteractions(mocksCollector.getMocks());         a3.doSomething();         verifyNoMoreInteractions(mocksCollector.getMocks()); // fail     } } 

or with annotations :

@RunWith(MockitoJUnitRunner.class) public class A2Test {     private final MocksCollector mocksCollector = new MocksCollector();      @Mock     private A a1;     @Mock     private A a2;     @Mock     private A a3;      @Test     public void test() throws Exception {         assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);         verifyNoMoreInteractions(mocksCollector.getMocks());         a2.doSomething();         verifyNoMoreInteractions(mocksCollector.getMocks()); // fail     } } 

It works but it adds a dependency on mockito internal.

like image 186
gontard Avatar answered Sep 16 '22 22:09

gontard