Writing parameterized tests using JUnit's @ParameterizedTest annotation

JUnit is a popular testing framework for Java applications. It provides various annotations and features to write effective and organized tests. One essential feature offered by JUnit is the ability to write parameterized tests using the @ParameterizedTest annotation. This allows developers to test their code with different input values without writing multiple test cases manually.

What are parameterized tests?

Parameterized tests are a way to write a single test method that can be executed multiple times with different input values. For example, if you have a method that performs a mathematical calculation, you can write a parameterized test to test it with various input values and compare the expected output.

An example

Let's consider a simple example to demonstrate how to write parameterized tests using the @ParameterizedTest annotation. Suppose you have a method sum() that adds two numbers and returns the result. You want to test it with multiple input values.

public class MathUtils {
    public int sum(int a, int b) {
        return a + b;

To write a parameterized test for the sum() method, you need to follow these steps:

  1. Add the JUnit Jupiter dependency to your project, if not already present.
  2. Import the necessary JUnit and other required classes.
  3. Create a test class and annotate it with the @RunWith annotation, specifying the Parameterized.class as the test runner.
  4. Define a public static method that returns a Stream of arguments. This method will provide the input values for the parameterized test.
  5. Annotate the test method with @ParameterizedTest and pass the name of the method that provides the input values using the value attribute.
  6. In the test method, define the required input parameters and assertions.

Here's how the parameterized test for the sum() method looks like:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MathUtilsTest {

    public void testSum(int a, int b, int expectedSum) {
        MathUtils mathUtils = new MathUtils();
        int result = mathUtils.sum(a, b);
        assertEquals(expectedSum, result);

    private static Stream<Arguments> provideNumbers() {
        return Stream.of(
                Arguments.of(3, 5, 8),
                Arguments.of(10, -2, 8),
                Arguments.of(0, 0, 0),
                Arguments.of(-5, -7, -12)

In this test, the provideNumbers() method returns a Stream of Arguments. Each Arguments object represents a set of input values for the sum() method. The test method testSum() is then annotated with @ParameterizedTest and linked to the provideNumbers() method. It takes the input values from the Arguments objects, executes the sum() method, and compares the result with the expected sum using assertEquals().

Benefits of parameterized tests

Writing parameterized tests offers several benefits:

  1. Code reusability: You can write a single test method and test the code with multiple input values. This promotes code reusability and reduces duplication.
  2. Improved test coverage: By testing the code with various input values, you can increase the overall test coverage and ensure that the code handles different scenarios correctly.
  3. Better test organization: Parameterized tests help in organizing tests into a single method, making it easier to manage and maintain the test suite.
  4. Enhanced readability: The use of parameterized tests can make the test code more expressive and easier to understand. The input values can be clearly defined, improving the readability of test cases.

In conclusion, JUnit�s @ParameterizedTest annotation provides a powerful way to write parameterized tests in Java applications. By using this feature, you can test your code with different input values and ensure its correctness and robustness. Parameterized tests offer benefits like code reusability, improved test coverage, better organization, and enhanced readability of test cases.

© NoobToMaster - A 10xcoder company