Mockito.verify()

Verify interactions with your mock

Mockito Verify is used to verify interaction of your mock inside your source (System Under Test) code.

We can check if method a is called with our mock, how many times it was called, or it was not called at all.

// Verify interactions
Mockito.verify(mock, Mockito.times(1)).sampleMethod();

// Verify, method should not be invoked even once
Mockito.verify(mock, Mockito.never()).sampleMethod2();

Mockito support 2 more verification methods,

  • Mockito.verifyNoMoreInteractions()

// Verify, we did not miss any interaction in out test
// If will fail if there are any interaction
// and we did not wrote a verification assertion in our test
Mockito.verifyNoMoreInteractions(otherMockedClass);
  • Mockito.verifyNoInteractions()

// Verify, our mock did not have any methods called in peice of code we were testing
Mockito.verifyNoInteractions(otherMockedClass);

Last updated