0
0
JUnittesting~10 mins

@ParameterizedTest annotation in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit 5
JUnit
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");
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test runner starts and identifies the @ParameterizedTest methodJUnit test environment ready to run parameterized tests-PASS
2Runs testIsEvenWithEvenNumbers with input 2Method isEven called with 2assertTrue(isEven(2)) checks if 2 is evenPASS
3Runs testIsEvenWithEvenNumbers with input 4Method isEven called with 4assertTrue(isEven(4)) checks if 4 is evenPASS
4Runs testIsEvenWithEvenNumbers with input 6Method isEven called with 6assertTrue(isEven(6)) checks if 6 is evenPASS
5Runs testIsEvenWithEvenNumbers with input 8Method isEven called with 8assertTrue(isEven(8)) checks if 8 is evenPASS
6Runs testIsEvenWithEvenNumbers with input 10Method isEven called with 10assertTrue(isEven(10)) checks if 10 is evenPASS
7All parameterized test cases completed successfullyTest report shows all inputs passed-PASS
Failure Scenario
Failing Condition: If the isEven method returns false for any even number input
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @ParameterizedTest annotation do in this test?
ARuns the test method multiple times with different inputs
BRuns the test method only once
CSkips the test method
DRuns the test method only if a condition is true
Key Result
Using @ParameterizedTest with @ValueSource helps test the same logic with multiple inputs efficiently, reducing code duplication and improving test coverage.