If we have this code:
@Test
public void test1(){
Interface1 i1 = mock(Interface1.class)
method1(); // This method calls i1.mockedmethod()
verify(i1, times(1)).mockedmethod();
method1();
verify(i1, times(2)).mockedmethod();
}
I know that it will pass the first verify, but I'm in doubt with the second one. Does verify method counts all the times that the method has been called or it only counts it since the last verify?
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.
Using the verify() Method Mockito provides us with a verify() method that lets us verify whether the mock void method is being called or not. It lets us check the number of methods invocations. So, if the method invocation returns to be zero, we would know that our mock method is not being called.
Master Java Unit testing with Spring Boot and Mockito Mockito provides the capability to a reset a mock so that it can be reused later.
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.
Note that it is possible to reset the times a method was called with Mockito.reset(mock)
Update: as suggested below by t7tran,using clearInvocations(T... mocks) will reset only the number of invocations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With