Mockito BDDMockito given()

1. Overview

The given() method provided by BDDMockito is a key element in enabling Behavior-Driven Development (BDD) style testing with Mockito. This method helps create a readable and expressive setup for your test, adhering to the "Given-When-Then" structure of BDD. This tutorial offers a focused look into the given() method, explaining its use, advantages, and nuances.

2. Development Steps

1. Set up a new Maven project.

2. Add the necessary Mockito and JUnit 5 dependencies.

3. Create a simple service class called BookService for our demonstration.

4. Design a BDD-style test for BookService utilizing BDDMockito's given().

5. Execute the test and interpret the outcome.

6. Discuss the importance and benefits of the given() 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 basic BookService class for the tutorial
class BookService {
    String getBookTitleById(int id) {
        // In a real-world scenario, this might fetch data from a database
        return "Dummy Title";
    }
}
// BDD-style test class for BookService using BDDMockito's given()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
public class BookServiceBDDTest {
    @Test
    public void testGetBookTitleById() {
        // Given: Set up the scenario
        BookService mockBookService = mock(BookService.class);
        int testId = 123;
        given(mockBookService.getBookTitleById(testId)).willReturn("Test Title");
        // When: Execute the method under test
        String title = mockBookService.getBookTitleById(testId);
        // Then: Verify the outcome
        System.out.println("Retrieved title: " + title);
    }
}

Output:

Retrieved title: Test Title

Code Explanation:

1. We've defined a basic BookService class with a method getBookTitleById().

2. The BookServiceBDDTest class is formulated to test BookService in a BDD manner using BDDMockito.

3. BDDMockito's given() method is used to set up the expected behavior. In our example, we've mocked the getBookTitleById() method to return a test title for a given test ID.

4. The action or the actual behavior we're testing is represented by the "When" section. Here, we invoke the mocked getBookTitleById() method and capture its return value.

5. The printed result confirms our mocked behavior and showcases the test's outcome.

5. Conclusion

The given() method from BDDMockito plays a pivotal role in establishing a clear and concise BDD-style test scenario in Mockito. It predefines the mocked behavior, making the subsequent stages of the test (When and Then) more predictable and straightforward. 

Embracing the BDD structure using given() not only enhances test readability but also aligns closely with the natural language-based requirements often used in agile projects.

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