JUnit 5 Parameterized Tests

Upasana | July 22, 2020 | 3 min read | 635 views | java junit


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

Gradle Setup

First of all you need to setup the latest Junit 5 Jupiter dependencies in build.gradle.

build.gradle
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2') (1)
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
1 You can grab the latest version of Junit 5 Jupyter from Maven Repository.

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 you just want to change the input data points for the tests while the test logic in itself remains exactly the same.

In this article we will cover different options for writing Junit 5 Parameterized Tests:

  • CsvSource Provider

  • MethodSource Provider

  • ValueSource Provider

  • CsvFileSource

  • EnumSource

1. CsvSource Provider

You 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}")    (1)
    @CsvSource({    (2)
            "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");
    }
}
1 This annotation is required to activate the parameterized test. Name of the testcase will be dynamically created using {0} = Palindrome is {1} where {0} is the first column in csv and {1} is the second column in csv.
2 Comma separated values (csv) with two columns in one row. A testcase will be create and executed for each row in csv, supplying the values as parameters to testcase. First column in csv will be mapped to number argument and second column in csv will be mapped to expectedResult argument of testcase.

2. 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")
@MethodSource("palindromeProvider")
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);
}

3. 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");
}

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


Top articles in this category:
  1. Junit interview questions for SDET automation engineer
  2. Migrating Spring Boot tests from Junit 4 to Junit 5
  3. Junit 5 Platform Launcher API
  4. Writing a simple Junit 5 test
  5. 50 Java Interview Questions for SDET Automation Engineer
  6. Rest Assured API Testing Interview Questions
  7. Creating custom Tag in Junit5 based tests

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.