Mockito Stubbing with Specific Arguments

Introduction

In this chapter, we will learn about stubbing methods with specific arguments using Mockito. Stubbing with specific arguments allows you to define precise behavior for mock objects when they are called with particular inputs. This helps you test how your code interacts with its dependencies under specific conditions.

What is Stubbing?

Stubbing is the process of specifying what mock objects should do when their methods are called. By defining the behavior of mock objects, you can control their interactions and ensure that your tests are reliable and predictable. Stubbing allows you to return specific values, throw exceptions, or execute custom logic when a method is called.

Stubbing with Specific Arguments

You can stub methods to return specific values when they are called with particular arguments. This allows you to create more precise and controlled tests.

Example: Calculator Class and CalculatorTest Class

Class Under Test: Calculator

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }
}

Test Class: CalculatorTest

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {

    @Test
    void testStubbingWithArguments() {
        // Create a mock object of Calculator
        Calculator calculator = mock(Calculator.class);

        // Stub the add method with specific arguments
        when(calculator.add(2, 3)).thenReturn(5);
        when(calculator.add(5, 4)).thenReturn(9);

        // Use the mock object
        int result1 = calculator.add(2, 3);
        int result2 = calculator.add(5, 4);

        // Assert the results
        assertEquals(5, result1);
        assertEquals(9, result2);

        // Verify the interactions
        verify(calculator).add(2, 3);
        verify(calculator).add(5, 4);
    }

    @Test
    void testStubbingWithSpecificSubtraction() {
        // Create a mock object of Calculator
        Calculator calculator = mock(Calculator.class);

        // Stub the subtract method with specific arguments
        when(calculator.subtract(5, 3)).thenReturn(2);
        when(calculator.subtract(10, 4)).thenReturn(6);

        // Use the mock object
        int result1 = calculator.subtract(5, 3);
        int result2 = calculator.subtract(10, 4);

        // Assert the results
        assertEquals(2, result1);
        assertEquals(6, result2);

        // Verify the interactions
        verify(calculator).subtract(5, 3);
        verify(calculator).subtract(10, 4);
    }
}

Explanation

  1. Creating a Mock:
    Calculator calculator = mock(Calculator.class);
    

    This line creates a mock object of the Calculator class.

  2. Stubbing with Specific Arguments:
    when(calculator.add(2, 3)).thenReturn(5);
    when(calculator.add(5, 4)).thenReturn(9);
    

    These lines define the behavior of the add method. When the add method is called with arguments 2 and 3, it will return 5. When called with 5 and 4, it will return 9.

  3. Using the Mock Object:
    int result1 = calculator.add(2, 3);
    int result2 = calculator.add(5, 4);
    

    These lines call the add method on the mock object with the specified arguments and store the results.

  4. Asserting the Results:
    assertEquals(5, result1);
    assertEquals(9, result2);
    

    These lines check if the results are as expected.

  5. Verifying Interactions:
    verify(calculator).add(2, 3);
    verify(calculator).add(5, 4);
    

    These lines verify that the add method was called with the specified arguments.

  6. Stubbing and Testing Subtraction:
    when(calculator.subtract(5, 3)).thenReturn(2);
    when(calculator.subtract(10, 4)).thenReturn(6);
    

    These lines define the behavior of the subtract method. When the subtract method is called with arguments 5 and 3, it will return 2. When called with 10 and 4, it will return 6.

  7. Using the Mock Object for Subtraction:
    int result1 = calculator.subtract(5, 3);
    int result2 = calculator.subtract(10, 4);
    

    These lines call the subtract method on the mock object with the specified arguments and store the results.

  8. Asserting the Subtraction Results:
    assertEquals(2, result1);
    assertEquals(6, result2);
    

    These lines check if the results are as expected.

  9. Verifying Subtraction Interactions:
    verify(calculator).subtract(5, 3);
    verify(calculator).subtract(10, 4);
    

    These lines verify that the subtract method was called with the specified arguments.

Running Tests using IntelliJ IDEA

Mockito Stubbing with Specific Arguments

Conclusion

Stubbing methods with specific arguments using Mockito allows you to create precise and controlled tests. By defining how mock objects should behave when called with particular inputs, you can ensure that your tests are reliable and cover a wide range of scenarios. This approach helps you validate the behavior of your code under various conditions, making your tests more comprehensive and effective.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top