dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2') (1)
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
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
.
1 | You can grab the latest version of Junit 5 Jupyter from Maven Repository. |
Parameterized Tests
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.
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.
@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.
@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:
- Junit interview questions for SDET automation engineer
- Migrating Spring Boot tests from Junit 4 to Junit 5
- Junit 5 Platform Launcher API
- Writing a simple Junit 5 test
- 50 Java Interview Questions for SDET Automation Engineer
- Rest Assured API Testing Interview Questions
- Creating custom Tag in Junit5 based tests