Mockito BDDMockito willReturn()

1. Overview

The willReturn() method from BDDMockito is pivotal in shaping Behavior-Driven Development (BDD) style unit tests with Mockito. This method enables us to set up stubbed return values for mocked methods, ensuring predictable behavior that aligns with our testing scenarios. This tutorial aims to provide insight into the willReturn() method, illustrating its usage within a BDD context.

2. Development Steps

1. Set up a new Maven project.

2. Add the necessary Mockito and JUnit 5 dependencies.

3. Create a BookService class, which will be our object under test.

4. Craft a BDD-style test for the BookService, utilizing BDDMockito's willReturn().

5. Execute the test and scrutinize the results.

6. Discuss the significance and advantages of the willReturn() method for BDD testing.

3. Dependencies (Mockito and JUnit 5)

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

4. Code Program

// A simple BookService class for demonstration purposes
class BookService {
    String fetchBookTitleById(String bookId) {
        // In a real-world application, this method might interact with a database or an external service
        return "Sample Book";
// BDD-style test class for BookService using BDDMockito's willReturn()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
public class BookServiceBDDTest {
    public void testFetchBookTitle() {
        // Given: Setting up the scenario
        BookService mockBookService = mock(BookService.class);
        String dummyBookId = "123";
        willReturn("Mocked Book Title").given(mockBookService).fetchBookTitleById(dummyBookId);
        // When: Performing the action
        String returnedTitle = mockBookService.fetchBookTitleById(dummyBookId);
        // Then: Verifying the outcome
        System.out.println("Returned Book Title: " + returnedTitle);


Returned Book Title: Mocked Book Title

Code Explanation:

1. We initiated with a simple BookService class having a method named fetchBookTitleById().

2. The test class, BookServiceBDDTest, employs a BDD approach for testing BookService, capitalizing on BDDMockito.

3. Within the test, we leverage the willReturn() function from BDDMockito to stub the return value of our fetchBookTitleById() method. This ensures that, for the specified input, our mocked method will always return the value we've dictated.

4. Subsequently, the "When" section carries out the action we wish to inspect. Here, we invoke the fetchBookTitleById() method on our mock.

5. The output section captures and prints the returned value, verifying that our mock behaves as anticipated.

5. Conclusion

By using BDDMockito's willReturn() function, we can craft BDD-style unit tests that establish explicit expectations about method return values. This allows us to control the behavior of our mocks, guaranteeing consistent test outcomes regardless of external factors. 

By incorporating willReturn(), we are better equipped to design transparent, predictable, and behavior-centric tests, ensuring our applications align closely with their intended behaviors and requirements.

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