0
0
JUnittesting~10 mins

Multiple parameter types in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies a method that accepts multiple parameter types using JUnit's parameterized test feature. It checks that the method correctly processes different combinations of input values.

Test Code - JUnit 5
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class CalculatorTest {

    public static int add(int a, int b) {
        return a + b;
    }

    @ParameterizedTest
    @CsvSource({
        "1, 2, 3",
        "-1, 1, 0",
        "100, 200, 300",
        "0, 0, 0"
    })
    void testAddMultipleParameters(int a, int b, int expected) {
        assertEquals(expected, add(a, b));
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initializes CalculatorTest class-PASS
2Runs parameterized test with parameters (1, 2, 3)Method add(1, 2) is calledCheck if add(1, 2) == 3PASS
3Runs parameterized test with parameters (-1, 1, 0)Method add(-1, 1) is calledCheck if add(-1, 1) == 0PASS
4Runs parameterized test with parameters (100, 200, 300)Method add(100, 200) is calledCheck if add(100, 200) == 300PASS
5Runs parameterized test with parameters (0, 0, 0)Method add(0, 0) is calledCheck if add(0, 0) == 0PASS
6All parameterized tests completedTest suite finished with all assertions passed-PASS
Failure Scenario
Failing Condition: The add method returns incorrect sum for any parameter set
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @CsvSource annotation provide in this test?
AMultiple sets of parameters for the test method
BA single parameter value for the test
CThe expected output only
DThe test method name
Key Result
Using parameterized tests with multiple parameter types helps efficiently verify method behavior with various inputs, reducing repetitive code and improving test coverage.