Mockito doNothing()

1. Overview

In Mockito, the doNothing() method is used to stub a void method that does nothing. It is particularly useful in scenarios where you want to suppress a method's behavior, especially if it's a void method that performs some actions that you don't want to execute during your unit tests. In this tutorial, we'll walk through how to use doNothing() effectively in Mockito.

2. Development Steps

1. Set up a new Maven project.

2. Add Mockito and JUnit 5 dependencies.

3. Create a Notifier class that contains a void method.

4. Write a test using Mockito and JUnit 5 to mock the behavior of the Notifier's method.

5. Run the test.

3. Dependencies (Mockito and JUnit 5)

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

4. Code Program

// Class containing a void method we want to suppress
class Notifier {
    public void notifyUser(String message) {
        System.out.println("Notification sent: " + message);
    }
}
// Test class using Mockito and JUnit 5
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
public class NotifierTest {
    @Test
    public void testNotifyUser() {
        Notifier mockNotifier = mock(Notifier.class);
        // Suppressing the method's behavior
        doNothing().when(mockNotifier).notifyUser(anyString());
        mockNotifier.notifyUser("Hello, User!");
        verify(mockNotifier).notifyUser("Hello, User!");
    }
}

Output:

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Code Explanation:

1. We start by defining a Notifier class with a method notifyUser that prints a message. In real-life scenarios, this method might send an email or push notification which we don't want to trigger during unit tests.

2. In the NotifierTest class, we create a mock of the Notifier class.

3. Using doNothing().when(mockNotifier).notifyUser(anyString());, we suppress the actual behavior of the notifyUser method. This ensures that when the method is called on the mock, it won't execute the actual System.out.println.

4. We then call the notifyUser method on our mock and finally verify that the method was indeed called with the specified argument.

5. Conclusion

Mockito's doNothing() method is a handy tool when we want to suppress the execution of void methods during unit testing. This capability ensures that we can test without invoking undesired side effects. In this tutorial, we demonstrated its use and showcased how to ensure that void methods are correctly suppressed during test execution.

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