Mockito times()

1. Overview

In unit testing, there are scenarios where it becomes important to ensure a method was invoked a specific number of times. Mockito provides the times() method, which works in conjunction with verify() to check the number of invocations on mock methods. This tutorial presents a deep dive into the usage of the times() method in Mockito.

2. Development Steps

1. Initialize a new Maven project.

2. Add the necessary dependencies: JUnit 5 and Mockito.

3. Create a class that features a method to be invoked multiple times.

4. Construct a test class for the previously formed class.

5. Leverage the verify() method in tandem with times() to verify the number of method invocations.

6. Design and execute the test case.

7. Assess the test results.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<!-- Mockito -->

4. Code Program

// Step 3: Design the LoggerService class
class LoggerService {
    void logMessage(String message) {
        // Typically, this method might log a message somewhere
// Step 4 and 5: Design a test class for LoggerService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
public class LoggerServiceTest {
    public void logMessageTest() {
        // Generate a mock of LoggerService
        LoggerService mockService = mock(LoggerService.class);
        // Invoke the mocked method multiple times
        // Use verify() along with times() to confirm the number of invocations
        verify(mockService, times(3)).logMessage("Test1");


The logMessageTest will pass successfully, proving that the times() method, combined with verify(), correctly ascertained the number of method calls on the mocked LoggerService.

Code Explanation:

1. After the seamless integration of Mockito and JUnit 5, we envisaged a simple LoggerService class possessing a logMessage method. This method, in real scenarios, could log messages to some system.

2. In our test suite, a mock of the LoggerService class was crafted using Mockito's mock() method.

3. We then invoked the logMessage method of the mock object thrice.

4. To check the number of invocations, we implemented the verify() method combined with times(). Specifically, we ascertained that the logMessage method with the "Test1" argument was invoked three times. If it wasn't called thrice, our test would fail. But in our demo, it passed as expected.

5. Conclusion

The times() method in Mockito is a powerful tool when combined with verify(). It gives developers the assurance that methods on mock objects are invoked the expected number of times. This further fortifies the reliability of unit tests and ensures a method's behavior aligns with its expected call frequency. When used judiciously, times() can enhance the accuracy and robustness of your test suites.

Related Mockito Methods

Mockito mock()
Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers