Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - what does verify method do?

Let's say i have the following psuedo like test code:

 //Let's import Mockito statically so that the code looks clearer  import static org.mockito.Mockito.*;   //mock creation  List mockedList = mock(List.class);   //using mock object  mockedList.add("one");  mockedList.clear();   //what do these two verify methods do ?  verify(mockedList).add("one");  verify(mockedList).clear(); 

I keep showing the test passed but i dont know what the verify means ? what is it verifying exactly ? I understand that i mocked a call to add and clear but what does the two verify calls do ?

like image 941
j2emanue Avatar asked Dec 13 '14 08:12

j2emanue


People also ask

What is verify in unit test?

Asserts are used to validate that properties of your system under test have been set correctly, whereas Verify is used to ensure that any dependencies that your system under test takes in have been called correctly.

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.


2 Answers

Mockito.verify(MockedObject).someMethodOnTheObject(someParametersToTheMethod); verifies that the methods you called on your mocked object are indeed called. If they weren't called, or called with the wrong parameters, or called the wrong number of times, they would fail your test.

like image 55
kinbiko Avatar answered Sep 25 '22 17:09

kinbiko


It asserts that the method was called, and with those arguments.

Comment out:

//mockedList.add("one"); 

Or change its argument and the test will fail.

like image 26
weston Avatar answered Sep 22 '22 17:09

weston