Test Overview
This test uses JUnit's @ArgumentsSource annotation with a custom provider to supply multiple sets of arguments. It verifies that the tested method correctly processes each input and produces the expected output.
This test uses JUnit's @ArgumentsSource annotation with a custom provider to supply multiple sets of arguments. It verifies that the tested method correctly processes each input and produces the expected output.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.ArgumentsProvider; import java.util.stream.Stream; public class CustomArgumentsSourceTest { static class CustomProvider implements ArgumentsProvider { @Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { return Stream.of( Arguments.of(2, 4), Arguments.of(3, 9), Arguments.of(4, 16) ); } } @ParameterizedTest @ArgumentsSource(CustomProvider.class) void testSquareFunction(int input, int expected) { int actual = input * input; Assertions.assertEquals(expected, actual, "Square calculation failed"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | - | PASS |
| 2 | JUnit identifies @ParameterizedTest with @ArgumentsSource(CustomProvider.class) | Test method 'testSquareFunction' ready to run with arguments from CustomProvider | - | PASS |
| 3 | CustomProvider.provideArguments() called to supply test arguments | Stream of Arguments: (2,4), (3,9), (4,16) | - | PASS |
| 4 | Test method invoked with input=2, expected=4 | Calculating 2 * 2 | Assert actual (4) equals expected (4) | PASS |
| 5 | Test method invoked with input=3, expected=9 | Calculating 3 * 3 | Assert actual (9) equals expected (9) | PASS |
| 6 | Test method invoked with input=4, expected=16 | Calculating 4 * 4 | Assert actual (16) equals expected (16) | PASS |
| 7 | All parameterized test cases completed | Test run summary available | - | PASS |