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
- Creating a Mock:
Calculator calculator = mock(Calculator.class);
This line creates a mock object of the
Calculator
class. - 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 theadd
method is called with arguments2
and3
, it will return5
. When called with5
and4
, it will return9
. - 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. - Asserting the Results:
assertEquals(5, result1); assertEquals(9, result2);
These lines check if the results are as expected.
- 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. - 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 thesubtract
method is called with arguments5
and3
, it will return2
. When called with10
and4
, it will return6
. - 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. - Asserting the Subtraction Results:
assertEquals(2, result1); assertEquals(6, result2);
These lines check if the results are as expected.
- 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
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.