Mockito doReturn()

1. Overview

The Mockito.doReturn() method provides an alternative way to stub method calls on mock objects. This technique is especially useful when we need to stub methods on spies or to stub void methods. In this tutorial, we'll explore the doReturn() method and its practical applications.

2. Development Steps

1. Create a new Maven project.

2. Add the required dependencies for Mockito and JUnit 5.

3. Develop a simple class (Calculator) which we'll use to demonstrate the doReturn() method.

4. Create a test class to showcase how to use Mockito.doReturn().

5. Execute the test cases and observe the results.

6. Analyze the outcomes.

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 -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

4. Code Program

// Step 3: Develop the Calculator class
class Calculator {
    int add(int a, int b) {
        return a + b;
    }
    void printSum(int a, int b) {
        System.out.println("Sum: " + add(a, b));
    }
}
// Step 4: Construct the test class for Calculator
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
public class CalculatorTest {
    @Test
    public void testDoReturn() {
        // Generate a mock of Calculator
        Calculator mockCalculator = mock(Calculator.class);
        // Use doReturn to stub the add method
        doReturn(5).when(mockCalculator).add(2, 3);
        // Execute the printSum method
        mockCalculator.printSum(2, 3);
        // Verify if the stubbed method was called
        verify(mockCalculator).add(2, 3);
    }
}

Output:

The test case testDoReturn will pass, confirming that the method add(2, 3) on the mock Calculator was called and stubbed correctly using doReturn().

Code Explanation:

1. First, we have a simple Calculator class with an add method that returns the sum of two integers and a printSum method that prints the sum.

2. In the test class, we created a mock of the Calculator using Mockito's mock() method.

3. We then stubbed the add method using the doReturn().when() idiom. This means that when add(2, 3) is called on the mock object, it will return 5 regardless of the actual implementation of the add method.

4. We called the printSum(2, 3) method on the mock, which internally calls the stubbed add method.

5. Finally, we used Mockito's verify() method to confirm that the add(2, 3) method was indeed called on the mock.

5. Conclusion

The Mockito.doReturn() method offers a flexible and powerful way to stub method calls on mock objects, especially when dealing with spies or stubbing void methods. Its usage can simplify the process of setting up mock behaviors, ensuring that your unit tests remain focused and effective.

Related Mockito Methods

Mockito mock()
Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers

Comments