Test Overview
This test uses the @ParameterizedTest annotation to run the same test method multiple times with different input values. It verifies that the isEven method correctly identifies even numbers.
This test uses the @ParameterizedTest annotation to run the same test method multiple times with different input values. It verifies that the isEven method correctly identifies even numbers.
import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class NumberUtilsTest { public static boolean isEven(int number) { return number % 2 == 0; } @ParameterizedTest @ValueSource(ints = {2, 4, 6, 8, 10}) void testIsEvenWithEvenNumbers(int number) { assertTrue(isEven(number), "Number should be even"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and identifies the @ParameterizedTest method | JUnit test environment ready to run parameterized tests | — | PASS |
| 2 | Runs testIsEvenWithEvenNumbers with input 2 | Method isEven called with 2 | assertTrue(isEven(2)) checks if 2 is even | PASS |
| 3 | Runs testIsEvenWithEvenNumbers with input 4 | Method isEven called with 4 | assertTrue(isEven(4)) checks if 4 is even | PASS |
| 4 | Runs testIsEvenWithEvenNumbers with input 6 | Method isEven called with 6 | assertTrue(isEven(6)) checks if 6 is even | PASS |
| 5 | Runs testIsEvenWithEvenNumbers with input 8 | Method isEven called with 8 | assertTrue(isEven(8)) checks if 8 is even | PASS |
| 6 | Runs testIsEvenWithEvenNumbers with input 10 | Method isEven called with 10 | assertTrue(isEven(10)) checks if 10 is even | PASS |
| 7 | All parameterized test cases completed successfully | Test report shows all inputs passed | — | PASS |
@ParameterizedTest annotation in JUnit?@ParameterizedTest using @ValueSource?@ParameterizedTest
@ValueSource(strings = {"apple", "banana"})
void testLength(String fruit) {
System.out.println(fruit.length());
}@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testIsPositive(String number) {
assertTrue(Integer.parseInt(number) > 0);
}@ParameterizedTest with multiple inputs including 0, -2, and 3. Which of the following test method signatures and annotations correctly achieve this?Pass the test and this pattern moves unknown