Open In App

JUnit 5 – @ParameterizedTest

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Parameter Sources: The JUnit 5 supports various sources for providing input for the @ParameterizedTest method. Commonly used sources are @ValueSource provides a single Parameterized value for primitive or String value, @EnumSource which is used for enum types, @MethodSource is used for invokes a method to provide the parameters for testing method.
  • Custom Conversion: By using Custom Conversion, we can be able to define customize Conversion to convert parameters to the types expected by the test method.
  • Display Names: We can be able to create customize Display Names of the Parameterized tests to make the output more meaningful. And it helps to tester to understand the methods in script.
  • Argument Accessors: You can use argument accessors to customize how parameters are accessed and passed to the test method.
  • Null and Empty String Values: We can use null or empty string values in the parameterized tests to cover additional Sequence of events. The @NullSource and @EmptySource annotations can be used for this purpose.
  • Exception Testing: Every Application needed exception handling then only the application can handle the unexpected errors in the functionality we can use the @ParameterizedTest annotation in combination with @Test to perform parameterized exception testing.

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:

Java




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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads