Mockito BDDMockito then()

1. Overview

Mockito's BDDMockito class has reshaped the way we write unit tests, bringing the Behavior-Driven Development (BDD) style into the limelight. One of its essential methods is then(), which offers a BDD-friendly way to verify mock interactions. This tutorial will provide a comprehensive introduction to the then() method in BDDMockito and how it can be effectively integrated into BDD-style tests.

2. Development Steps

1. Set up a Maven project.

2. Add the necessary Mockito and JUnit 5 dependencies.

3. Create a Messenger class that will serve as the target of our testing.

4. Construct a BDD-style test for the Messenger using BDDMockito's then().

5. Execute the test and analyze the results.

6. Understand the importance and practical applications of the then() method in BDDMockito.

3. Dependencies (Mockito and JUnit 5)

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

4. Code Program

// The Messenger class to be tested
class Messenger {
    public String sendMessage(String recipient, String message) {
        // Real-world scenarios would involve sending a message to a recipient
        return "Message sent to " + recipient;
// BDD-style test class for Messenger using BDDMockito's then()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
import static org.junit.jupiter.api.Assertions.*;
public class MessengerBDDTest {
    public void testSendMessage() {
        // Given: Creating the mock and setting up the scenario
        Messenger mockMessenger = mock(Messenger.class);
        given(mockMessenger.sendMessage(anyString(), anyString())).willReturn("Mocked Message");
        // When: Performing the action
        String result = mockMessenger.sendMessage("John", "Hello!");
        // Then: Verifying the mock interaction
        then(mockMessenger).should().sendMessage("John", "Hello!");
        assertEquals("Mocked Message", result);


No errors. (The test is successful.)

Code Explanation:

1. We introduced a basic Messenger class with a sendMessage() method that simulates sending messages.

2. Inside the MessengerBDDTest class, we embraced the BDD approach using BDDMockito.

3. After setting up a mock scenario, we verified the interaction with the mock using the then() method.

4. The line then(mockMessenger).should().sendMessage("John", "Hello!"); asserts that the sendMessage() method was called with the provided arguments.

5. The test confirms that our mock's behavior aligns with our expectations, and the then() method verifies mock interactions in a BDD-friendly manner.

5. Conclusion

BDDMockito's then() method offers a concise, readable, and BDD-compatible approach to validating mock interactions in Mockito. By blending seamlessly into the Given-When-Then structure of BDD, it allows developers to write clear and expressive tests. Mastering then() ensures you're on the right path to crafting robust and maintainable BDD-style unit tests.

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()