Mockito BDDMockito willCallRealMethod()

1. Overview

BDDMockito's willCallRealMethod() is an instrumental method in Mockito when practicing Behavior-Driven Development (BDD). Sometimes, while writing tests, you want to mock certain methods of a class but allow others to execute their real implementation. The willCallRealMethod() comes in handy for these scenarios, making it a valuable tool in a tester's arsenal. This tutorial shows how to efficiently use the willCallRealMethod() in BDD-style unit testing using Mockito.

2. Development Steps

1. Set up a Maven project.

2. Incorporate the required Mockito and JUnit 5 dependencies.

3. Construct a Calculator class, which will be the focal point of our testing.

4. Write a BDD-style test for the Calculator, applying BDDMockito's willCallRealMethod().

5. Execute the test and interpret the output.

6. Comprehend the significance and practical application of the willCallRealMethod() in BDD tests.

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 Calculator class to be tested
class Calculator {
    public double add(double a, double b) {
        return a + b;
    }
    public double subtract(double a, double b) {
        return a - b;
    }
}
// BDD-style test class for Calculator using BDDMockito's willCallRealMethod()
import org.junit.jupiter.api.Test;
import static org.mockito.BDDMockito.*;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorBDDTest {
    @Test
    public void testAddMethod() {
        // Given: Creating the mock and setting up the scenario
        Calculator mockCalculator = mock(Calculator.class);
        // Mocking subtract while letting add execute its real implementation
        given(mockCalculator.subtract(anyDouble(), anyDouble())).willReturn(0.0);
        willCallRealMethod().given(mockCalculator).add(anyDouble(), anyDouble());
        // When: Performing the actions
        double result = mockCalculator.add(3.0, 4.0);
        // Then: Verifying the results
        assertEquals(7.0, result);
    }
}

Output:

No errors. (The test passes successfully.)

Code Explanation:

1. We introduced a basic Calculator class with add() and subtract() methods.

2. Within the CalculatorBDDTest class, we adopted a BDD approach using BDDMockito.

3. In our test scenario, we decided to mock the subtract() method while allowing the add() method to execute its genuine behavior.

4. The line willCallRealMethod().given(mockCalculator).add(anyDouble(), anyDouble()); ensures that when we invoke the add() method on our mock, it will run its original code.

5. As we can observe in the output, our test passes, reinforcing the proper function of willCallRealMethod() and our expectations.

5. Conclusion

The willCallRealMethod() function in BDDMockito presents a flexible and powerful approach to managing mock behaviors in BDD-style testing with Mockito. 

By enabling certain methods to run their original implementations while other methods are mocked, developers can precisely control the behavior of their test environments. As a result, this technique fosters more robust and fine-tuned testing, ensuring that your software behaves as expected.

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