Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Mockito to verify that a method was never called


Can I use Mockito to verify that a method was never called


Yes, you can use Mockito to verify that a method was never called. Mockito provides several ways to achieve this, but the most straightforward and readable method is using the `never()` function.

Using `never()`

The `never()` function is specifically designed to verify that a method on a mock object was never called. Here's how you can use it:

java
import org.mockito.Mockito;
import static org.mockito.Mockito.*;

// Example class with methods to test
class Example {
    void methodToCall() {
        System.out.println("methodToCall was called");
    }

    void methodNotToCall() {
        System.out.println("methodNotToCall was called");
    }
}

public class MockitoTest {
    public static void main(String[] args) {
        Example mockExample = mock(Example.class);
        mockExample.methodToCall();
        
        // Verify that methodToCall was called once
        verify(mockExample, times(1)).methodToCall();
        
        // Verify that methodNotToCall was never called
        verify(mockExample, never()).methodNotToCall();
    }
}

In this example, `verify(mockExample, never()).methodNotToCall();` checks that `methodNotToCall()` was never called on the `mockExample` object.

Using `times(0)`

Another way to verify that a method was not called is by using the `times(0)` verification mode. This method is similar to `never()` but is less readable:

java
verify(mockExample, times(0)).methodNotToCall();

This approach also checks that `methodNotToCall()` was not called, but it's generally less preferred due to its readability compared to `never()`.

Choosing Between `never()` and `times(0)`

Both methods achieve the same goal, but `never()` is considered more readable and intuitive. It explicitly states that a method should never have been called, making the intent of the test clearer.

Additional Verification Methods

If you want to ensure that no methods were called on a mock object at all, you can use `verifyNoInteractions(mockObject)`. However, this is more general and checks all methods, not just a specific one.

java
verifyNoInteractions(mockExample);

This method is useful when you want to ensure that there were no interactions with the mock object whatsoever.

In summary, Mockito's `never()` function is the most straightforward way to verify that a specific method was never called, providing clear and readable test code.

Citations:
[1] https://intellipaat.com/blog/how-to-verify-that-a-specific-method-was-not-called-using-mockito/
[2] https://community.lambdatest.com/t/how-can-you-use-mockito-to-verify-that-a-specific-method-was-not-called-on-an-objects-dependency/31706
[3] https://davidvlijmincx.com/posts/mockito-verify/
[4] https://stackoverflow.com/questions/512254/use-mockito-to-verify-that-nothing-is-called-after-a-method/1056090
[5] https://stackoverflow.com/questions/12862659/how-to-verify-that-a-specific-method-was-not-called-using-mockito
[6] https://www.vogella.com/tutorials/Mockito/article.html
[7] https://www.wyzant.com/resources/answers/651999/how-to-verify-that-a-specific-method-was-not-called-using-mockito
[8] https://dzone.com/refcardz/mockito
[9] https://www.javaguides.net/2023/10/mockito-never.html