Mockito - what does verify method do?

13,190

Solution 1

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.

Solution 2

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.

Share:
13,190
j2emanue
Author by

j2emanue

A mobile developer of both IOS and Android platforms

Updated on June 14, 2022

Comments

  • j2emanue
    j2emanue almost 2 years

    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 ?