Skip to content

How to verify that a specific method was not called using Mockito?

To verify that a specific method was not called using Mockito, you can use the verify method with the never() argument matcher.

Here's an example of how you can use this method:

java
@Test
public void testMethod() {
  MyClass mock = mock(MyClass.class);
  // ...
  verify(mock, never()).someMethod();
}

This test will pass if the someMethod was not called on the mock object during the test. If the method was called, the test will fail with an error message indicating that the method was unexpectedly invoked.

Alternatively, you can use times(0) to achieve the same result:

java
@Test
public void testMethod() {
  MyClass mock = mock(MyClass.class);
  // ...
  verify(mock, times(0)).someMethod();
}

I hope this helps! Let me know if you have any questions.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.