Test Overview
This test uses a custom argument provider to supply multiple sets of input data to a parameterized test method. It verifies that the sum of two integers matches the expected result for each data set.
This test uses a custom argument provider to supply multiple sets of input data to a parameterized test method. It verifies that the sum of two integers matches the expected result for each data set.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.api.extension.ExtensionContext; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; class CustomArgumentProviderTest { static class SumArgumentsProvider implements ArgumentsProvider { @Override public Stream<Arguments> provideArguments(ExtensionContext context) { return Stream.of( Arguments.of(1, 2, 3), Arguments.of(5, 7, 12), Arguments.of(10, 15, 25) ); } } @ParameterizedTest @ArgumentsSource(SumArgumentsProvider.class) void testSum(int a, int b, int expectedSum) { int actualSum = a + b; assertEquals(expectedSum, actualSum, () -> String.format("Sum of %d and %d should be %d", a, b, expectedSum)); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and identifies the parameterized test method with custom argument provider | JUnit test environment initialized, SumArgumentsProvider ready to supply arguments | - | PASS |
| 2 | SumArgumentsProvider.provideArguments() is called to get test data stream | Stream of argument sets [(1,2,3), (5,7,12), (10,15,25)] prepared | - | PASS |
| 3 | Test method testSum is invoked with arguments (1, 2, 3) | Test method running with a=1, b=2, expectedSum=3 | assertEquals(3, 1+2) | PASS |
| 4 | Test method testSum is invoked with arguments (5, 7, 12) | Test method running with a=5, b=7, expectedSum=12 | assertEquals(12, 5+7) | PASS |
| 5 | Test method testSum is invoked with arguments (10, 15, 25) | Test method running with a=10, b=15, expectedSum=25 | assertEquals(25, 10+15) | PASS |
| 6 | All parameterized test invocations complete, test run ends | All assertions passed for all argument sets | - | PASS |