Home JUnit 5 - Parameterized Testing
Post
Cancel

JUnit 5 - Parameterized Testing

JUnit 5 - Parameterized Testing

Parameterized testing is an essential technique used in software testing to check how a program behaves under different input scenarios. It allows developers to run the same test with different sets of input values to verify that the program behaves as expected in all cases. In JUnit 5, parameterized testing is made easy with the use of the @ParameterizedTest annotation.

In this blog post, we will explore how to use parameterized testing with JUnit 5 and provide an example of how it can be used to test a simple method.

Example: Testing a Simple Method with Parameterized Testing

Let’s say we have a simple method that calculates the factorial of a given integer. The factorial of a number is the product of all the numbers from 1 to that number. For example, the factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120. Our method looks like this:

1
2
3
4
5
6
7
public static int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

To test this method using parameterized testing, we can create a test class and annotate a test method with the @ParameterizedTest annotation. We also need to provide a source for the parameterized values using the @ValueSource annotation. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class FactorialTest {

    @ParameterizedTest
    @ValueSource(ints = {0, 1, 2, 3, 4, 5})
    public void testFactorial(int n) {
        int expected = 1;
        for (int i = 1; i <= n; i++) {
            expected *= i;
        }
        int actual = Factorial.factorial(n);
        assertEquals(expected, actual);
    }
}

In this example, we have annotated our test method with @ParameterizedTest and provided a source of integer values using @ValueSource. The integers provided are 0, 1, 2, 3, 4, and 5. The test method iterates over each value and calculates the expected factorial using a loop. It then calls the factorial method with the current value and compares the result with the expected value using the assertEquals() assertion.

When we run this test, JUnit 5 will run the test method once for each parameter value provided by the @ValueSource annotation. This means that the test method will be run six times, once for each value in the source. If the test fails for any value, the test will be marked as a failure and the specific value that caused the failure will be reported.

Conclusion

Parameterized testing is a powerful technique that can help you create more thorough and maintainable unit tests for your Java applications. In this blog post, we have explored how to use parameterized testing with JUnit 5 and provided an example of how it can be used to test a simple method. By using parameterized testing, you can easily test your code with multiple input values, catch bugs early, and ensure that your code behaves as expected in all scenarios.

This post is licensed under CC BY 4.0 by the author.