Open In App

JUnit 5 – @ParameterizedTest

The JUnit 5 is one of the latest and most famous Java frameworks for testing. The JUnit Introduces a lot of features, the @ParameterizedTest Annotation is one of them for testing which allows us to run multiple times with different types of parameters. This feature helps the testers execute a single test method with different types of parameters. In this article, we will learn how the @ParameterizedTest works in testing. For JUnit 5 we need junit-jupiter-params artifact from JUnit 5 Platform. This artifact is available in Maven Gradle and others also.

Dependencies

For the maven project category, add the below dependencies:



<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

For Gradle project category,

testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.1")

Features of @ParameterizedTest

Syntax:

@ParameterizedTest(name = "test name pattern - { }: Description of the test case
@AnnotationSource({"parameterSet1", "parameterSet2", ...})
void testMethod(Type parameter1, Type parameter2, ...) {
// Test logic using provided parameters
}

Example:






import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
  
class ParameterizedTestExample1 {
  
    @ParameterizedTest(name = "Addition Test #{index} - {0} + {1} = {2}")
    @CsvSource({"1, 1, 2", "2, 3, 5", "5, 5, 10"})
    void testAddition(int a, int b, int expectedResult) 
    {
        Calculator calculator = new Calculator();
        int result = calculator.add(a, b);
        assertEquals(expectedResult, result, () -> {
            return a + " + " + b + " should equal " + expectedResult + ", but the result is " + result;
        });
    }
  
    private static class Calculator {
        int add(int a, int b) {
            return a + b;
        }
    }
}

Explanation of the above Program:

@ParameterizedTest(name = “Addition Test index – 0 1 = 2 ” ): This is the @ParameterizedTest annotation, The name characteristic is used to customise the display call of the test. In this example, the show call is about to “Addition Test # index – 0 1 = 2 “. The placeholders index , zero , 1 , and 2 are parameters with a purpose to get replaced with actual values throughout test execution.

This is the @CsvSource annotation, which provides a comma-separated list of parameter sets for the test. Each string inside the curly braces represents a set of parameters for a single execution of the test method. In this situation there are three sets (1, 1, 2), (2, 3, 5) and (5, 5, 10). For each set of values, the values are separated by commas, and they correspond to the parameters of the testAddition.

Conclusion

@ParameterizedTest annotation in JUnit 5 is a powerful feature that allows you to write parameterized tests with various sets of input values. We have already provided the examples with clean explanation. @ParameterizedTest is a valuable feature that significantly contributes to the effectiveness, readability, and maintainability of our test suites.


Article Tags :