Mockito verify()

1. Overview

In unit testing, verifying that specific methods were called with expected arguments is essential. Mockito's verify() method facilitates this by allowing you to check method calls on mock objects. This tutorial provides insight into the verify() method and demonstrates how it can be effectively used in testing scenarios.

2. Development Steps

1. Initialize a new Maven project.

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

3. Design a class with a method to be invoked.

4. Construct a test class for the earlier designed class.

5. Use the verify() method to ensure that the method was called with the expected arguments.

6. Design and run the test cases.

7. Examine the test results.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
<!-- Mockito -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

4. Code Program

// Step 3: Draft the NotificationService class
class NotificationService {
    void sendNotification(String message, String recipient) {
        // Typically, this method might send a notification to the recipient
    }
}
// Step 4 and 5: Construct a test class for NotificationService
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class NotificationServiceTest {
    @Test
    public void sendNotificationTest() {
        // Create a mock of NotificationService
        NotificationService mockService = mock(NotificationService.class);
        // Call the mocked method
        mockService.sendNotification("Hello", "John");
        // Use verify() to ensure the method was called with the right parameters
        verify(mockService).sendNotification("Hello", "John");
    }
}

Output:

The sendNotificationTest will pass successfully, asserting that the verify() method was aptly utilized to confirm the method invocation on the mocked NotificationService.

Code Explanation:

1. After integrating Mockito and JUnit 5, we conceptualized a rudimentary NotificationService class that contains a sendNotification method, which, under genuine circumstances, might dispatch notifications.

2. Within our test suite, a mock of the NotificationService was instantiated using Mockito's mock() method.

3. After this, the sendNotification method of our mock object was invoked with certain parameters.

4. The crux of this tutorial, the verify() method, was then employed to ensure that the sendNotification method of our mock object was indeed called with the specified parameters. If the method was not invoked as expected, the test would fail, but in our case, it passed.

5. Conclusion

Mockito's verify() method emerges as a vital instrument for affirming method invocations on mock objects. This function not only ascertains that a particular method was invoked but also verifies that it was called with the right arguments, thereby bolstering the rigor and thoroughness of unit tests. By leveraging verify(), developers can cultivate more reliable and resilient applications.

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

Comments