Parameterized Tests using JUnit 5

Carvia Tech | October 27, 2019 | 2 min read | 365 views | java junit

In this article we will learn how to create Parameterized tests in JUnit 5 Jupiter using different data source providers such as CsvSource, CsvFileSource, MethodSource, EnumSource and ValueSource etc.

Gradle Setup

First of all we need to setup latest Junit Jupiter dependencies in build.gradle.

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.5.2') (1)

test {
    testLogging {
        events "passed", "skipped", "failed"

Parameterized Tests

Parameterized tests

Parameterized tests make it possible to run a test multiple times with different arguments. They are declared just like regular @Test methods but use the @ParameterizedTest annotation instead. In addition, you must declare at least one source that will provide the arguments for each invocation and then consume the arguments in the test method.

Source: Junit 5 docs

Parameterized tests are most helpful where we just want to change the input data points for the tests while the test logic in itself remains exactly the same.

CsvSource Provider

We can create Parameterized tests easily using a CSV source by using @CsvSource annotation and providing inline CSV.

CsvSource example
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

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

public class PalindromeTest {

    @ParameterizedTest(name = "{0} = Palindrome is {1}")
            "10,   false",
            "100,   false",
            "1001,  true",
            "212212, true"
    void testPalindrome(int number, boolean expectedResult) {
        Palindrome checker = new Palindrome();
        assertEquals(expectedResult, checker.isPalindrome(number),
                () -> expectedResult ? number + "  should be Palindrome" : number + "  should not be Palindrome");

MethodSource Provider

@MethodSource allows you to refer to one or more factory methods of the test class. Factory method must be declared static and must not accept any arguments.

MethodSource example
@ParameterizedTest(name = "Test {0} is a Palindrome")
void testWithExplicitLocalMethodSource(int argument) {
    Palindrome checker = new Palindrome();
    assertTrue(checker.isPalindrome(argument), "Number " + argument + " is a palindrome");

static Stream<Integer> palindromeProvider() {
    return Stream.of(121, 1001, 1240421);

ValueSource Provider

@ValueSource is one of the simplest possible sources where a single argument per parameterized test invocation can be provided in an array form.

ValueSource example
@ParameterizedTest(name = "Test {0} is a Palindrome")
@ValueSource(ints = { 121, 202, 3003 })
void testWithValueSource(int argument) {
    Palindrome checker = new Palindrome();
    assertTrue(checker.isPalindrome(argument), "Number " + argument + " is a palindrome");

We can use other sources like @CsvFileSource, @EnumSource in a similar fashion.

Top articles in this category:
  1. SDET: JUnit interview questions for automation engineer
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: Rest Assured Interview Questions
  4. Top 15 coding problems for SDET Java Interviews
  5. Migrating Spring Boot tests from Junit 4 to Junit 5
  6. Junit 5 Platform Launcher API
  7. Creating custom Tag in Junit5 based tests

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info