Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I use Mockito to verify that a method was called at least once


How do I use Mockito to verify that a method was called at least once


To verify that a method was called at least once using Mockito, you can use the `atLeastOnce()` verification mode. Here's a detailed explanation of how to do it:

Basic Syntax

The basic syntax for verifying that a method was called at least once involves using the `verify` method with the `atLeastOnce()` verification mode. Here is how you can do it:

java
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

// Assuming 'mockObject' is your mock object
verify(mockObject, atLeastOnce()).methodName();

Explanation

- Mock Object Creation: First, you need to create a mock object using Mockito's `mock()` method. This object will track any method calls made on it.

java
  // Create a mock object
  MyService mockService = mock(MyService.class);
  

- Method Invocation: During your test, you need to invoke the method on the mock object that you want to verify.

java
  // Invoke the method on the mock object
  mockService.doSomething();
  

- Verification: After invoking the method, you use the `verify` method to check if it was called at least once. The `atLeastOnce()` verification mode ensures that the method was called one or more times.

java
  // Verify that the method was called at least once
  verify(mockService, atLeastOnce()).doSomething();
  

Example

Here's a complete example:

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
public class MyTest {

    @Mock
    private MyService mockService;

    @Test
    void testMethodCalledAtLeastOnce() {
        // Invoke the method on the mock object
        mockService.doSomething();
        
        // Verify that the method was called at least once
        verify(mockService, atLeastOnce()).doSomething();
    }
}

Additional Verification Modes

Mockito provides several verification modes that you can use depending on your testing needs:

- `times(int)`: Verifies that a method was called exactly a specified number of times.
- `atLeast(int)`: Verifies that a method was called at least a specified number of times.
- `atMost(int)`: Verifies that a method was called at most a specified number of times.
- `never()`: Verifies that a method was never called.
- `atMostOnce()`: Verifies that a method was called at most once (i.e., zero or one time).

These modes can be used similarly to `atLeastOnce()` by passing them as the second argument to the `verify` method[1][3][4].

Citations:
[1] https://davidvlijmincx.com/posts/mockito-verify/
[2] https://stackoverflow.com/questions/14889951/how-to-verify-a-method-is-called-two-times-with-mockito-verify
[3] https://reflectoring.io/clean-unit-tests-with-mockito/
[4] https://www.dhiwise.com/post/mastering-unit-testing-a-complete-guide-to-mockito-verify
[5] https://github.com/mockito/mockito/issues/2166
[6] https://www.vogella.com/tutorials/Mockito/article.html
[7] https://www.tutorialspoint.com/mockito/mockito_verifying_behavior.htm
[8] https://github.com/mockito/mockito/issues/3014
[9] https://stackoverflow.com/questions/39452438/mockito-how-to-verify-a-method-was-called-only-once-with-exact-parameters-ignor
[10] https://www.digitalocean.com/community/tutorials/mockito-verify
[11] https://www.javacodegeeks.com/2015/11/mockito-verification.html