Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito: How to verify a method was called only once with exact parameters ignoring calls to other methods?

Using Mockito in Java how to verify a method was called only once with exact parameters ignoring calls to other methods?

Sample code:

public class MockitoTest {      interface Foo {         void add(String str);         void clear();     }       @Test     public void testAddWasCalledOnceWith1IgnoringAllOtherInvocations() throws Exception {         // given         Foo foo = Mockito.mock(Foo.class);          // when         foo.add("1"); // call to verify         foo.add("2"); // !!! don't allow any other calls to add()         foo.clear();  // calls to other methods should be ignored          // then         Mockito.verify(foo, Mockito.times(1)).add("1");         // TODO: don't allow all other invocations with add()          //       but ignore all other calls (i.e. the call to clear())     }  } 

What should be done in the TODO: don't allow all other invocations with add() section?

Already unsuccessfully tried:

  1. verifyNoMoreInteractions(foo);

Nope. It does not allow calls to other methods like clear().

  1. verify(foo, times(0)).add(any());

Nope. It does not take into account that we allow one call to add("1").

like image 298
Igor Mukhin Avatar asked Sep 12 '16 14:09

Igor Mukhin


People also ask

How do you verify a method called 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.

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.

How can specify the mock behavior of an object in Java?

We can mock an object using @Mock annotation too. It's useful when we want to use the mocked object at multiple places because we avoid calling mock() method multiple times. The code becomes more readable and we can specify mock object name that will be useful in case of errors.


2 Answers

Mockito.verify(foo, Mockito.times(1)).add("1"); Mockito.verify(foo, Mockito.times(1)).add(Mockito.anyString()); 

The first verify checks the expected parametrized call and the second verify checks that there was only one call to add at all.

like image 173
hunter Avatar answered Sep 28 '22 05:09

hunter


The previous answer can be simplified even further.

Mockito.verify(foo).add("1"); Mockito.verify(foo).add(Mockito.anyString()); 

The single parameter verify method is just an alias to the times(1)implementation.

like image 40
Travis Miller Avatar answered Sep 28 '22 07:09

Travis Miller