Mockito BDDMockito times()

1. Overview

In Behavior-Driven Development (BDD), it's often essential to ensure that a method is called a specific number of times. The BDDMockito.times() method comes in handy for this purpose, enabling precise verification of the method invocation count. In this tutorial, we'll explore the BDDMockito.times() method and demonstrate its role in BDD-style testing.

2. Development Steps

1. Set up a new Maven project.

2. Incorporate Mockito and JUnit 5 dependencies.

3. Create a Messenger class for testing purposes.

4. Develop a BDD-style test for Messenger employing BDDMockito.times().

5. Run the test and review the outcome.

6. Grasp the significance and application of BDDMockito.times() in verifying method call counts.

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

// The Messenger class we're aiming to test
class Messenger {
    public String sendMessage(String message) {
        // In a real-world application, this might send a message to a server or user
        return "Message sent: " + message;
    }
}
// BDD-style test class for Messenger using BDDMockito's times()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
import static org.junit.jupiter.api.Assertions.*;
public class MessengerBDDTest {
    @Test
    public void testSendMessage() {
        // Given: Setting up the mock and scenario
        Messenger mockMessenger = mock(Messenger.class);
        given(mockMessenger.sendMessage(anyString())).willReturn("Mocked Message Sent");
        // When: Taking action multiple times
        mockMessenger.sendMessage("Hello!");
        mockMessenger.sendMessage("Hello again!");
        // Then: Verify the interaction count with the mock
        then(mockMessenger).should(times(2)).sendMessage(anyString());
    }
}

Output:

No errors. (The test passes successfully.)

Code Explanation:

1. We begin with a simple Messenger class that has a sendMessage() method, emulating a message-sending operation.

2. Our BDD-style test within MessengerBDDTest class utilizes BDDMockito.

3. The line given(mockMessenger.sendMessage(anyString())).willReturn("Mocked Message Sent"); sets the expectation that any string argument passed to sendMessage() will yield "Mocked Message Sent".

4. The method sendMessage() is then invoked twice in succession.

5. In the verification phase, then(mockMessenger).should(times(2)).sendMessage(anyString()); ensures that sendMessage() was called precisely twice, irrespective of the message content.

6. BDDMockito.times() empowers us to assert exact method invocation counts, ensuring that our code behaves as intended.

5. Conclusion

Leveraging BDDMockito.times() in BDD tests with Mockito offers a meticulous way to verify the exact number of times a method is invoked. As observed, it's a powerful tool in our testing toolkit, enabling us to validate that our code's behavior aligns perfectly with our expectations.

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