How do you verify that a method was called in Mockito?

Beginner

Answer

Mockito provides the verify() method to check if methods were called:

// Verify method was called once
verify(mockList).add("item");

// Verify method was called specific number of times
verify(mockList, times(2)).add(anyString());

// Verify method was never called
verify(mockList, never()).clear();

// Verify method was called at least/at most certain times
verify(mockList, atLeast(1)).size();
verify(mockList, atMost(3)).add(anyString());

Verification should be done after the method under test has been executed.