Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - Does verify method reboot number of times?

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?

like image 665
CarlosMorente Avatar asked May 06 '15 15:05

CarlosMorente


People also ask

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.

How do you verify that void methods were called using Mockito?

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.

What does Mockito Reset do?

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.

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.


1 Answers

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

like image 82
OHY Avatar answered Sep 19 '22 15:09

OHY