Mockito BDDMockito willDoNothing()

1. Overview

BDDMockito's willDoNothing() method is a useful tool when dealing with void methods in a Behavior-Driven Development (BDD) style testing approach using Mockito. While mocking void methods can be tricky, willDoNothing() allows developers to ensure that a void method does not execute its original behavior when called. This tutorial will provide insights into how to utilize willDoNothing() effectively in BDD-style unit testing with Mockito.

2. Development Steps

1. Set up a Maven project.

2. Add the necessary Mockito and JUnit 5 dependencies.

3. Create a PrinterService class that will be the object under test.

4. Write a BDD-style test for the PrinterService, using BDDMockito's willDoNothing().

5. Execute the test and analyze the results.

6. Understand the significance and advantages of using the willDoNothing() method in BDD testing.

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 Core -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

4. Code Program

// A straightforward PrinterService class for demonstration
class PrinterService {
    void printMessage(String message) {
        // In a real-world scenario, this might interface with a physical printer or another output device
        System.out.println("Printing: " + message);
    }
}
// BDD-style test class for PrinterService using BDDMockito's willDoNothing()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
public class PrinterServiceBDDTest {
    @Test
    public void testPrintMessage() {
        // Given: Setting up the scenario
        PrinterService mockPrinter = mock(PrinterService.class);
        // Using willDoNothing() to ensure the void method doesn't execute its original behavior
        willDoNothing().given(mockPrinter).printMessage(anyString());
        // When: Executing the action
        mockPrinter.printMessage("Hello, World!");
        // Then: No output is produced, confirming our mock's behavior
        System.out.println("Finished testing printMessage method.");
    }
}

Output:

Finished testing printMessage method.

Code Explanation:

1. We introduced a basic PrinterService class that contains a printMessage() method.

2. The PrinterServiceBDDTest test class employs a BDD approach with BDDMockito.

3. In the test, willDoNothing() is used to ensure that the mocked printMessage() method does not execute its original behavior.

4. During the "When" phase, we invoke the printMessage() method on our mock, expecting no original behavior to occur.

5. The only result visible in the output segment is the "Finished testing printMessage method." message, signifying that our mock behavior was upheld.

5. Conclusion

BDDMockito's willDoNothing() method proves invaluable when creating BDD-style unit tests involving void methods. By enabling developers to bypass the original behavior of void methods, willDoNothing() enhances control over testing environments. This helps ensure that tests remain isolated and free from unintended side effects, fostering confidence in the test results.

Familiarity with tools like willDoNothing() is essential for anyone looking to harness the full power of BDD-style testing in Mockito.

Related Mockito BDDMockito Class Methods (Behavior-Driven Development Style)

Mockito BDDMockito
Mockito BDDMockito given()
Mockito BDDMockito willThrow()
Mockito BDDMockito willAnswer()
Mockito BDDMockito willReturn()
Mockito BDDMockito willDoNothing()
Mockito BDDMockito willCallRealMethod()
Mockito BDDMockito then()
Mockito BDDMockito.any()
Mockito BDDMockito.times()

Comments