@CsvSource lets us supply comma-separated values for each invocation of a @ParameterizedTest. The CSV entries are converted to arguments by type.
Java source
Definition of CsvSource(Version: junit-5 6.0.1) package org.junit.jupiter.params.provider;
........
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(CsvSources.class)
@Documented
@Inherited
@API(status = STABLE, since = "5.7")
@ArgumentsSource(CsvArgumentsProvider.class)
@SuppressWarnings("exports")
public @interface CsvSource {
String[] value() default {};
@API(status = STABLE, since = "5.10")
String textBlock() default "";
@API(status = STABLE, since = "5.10")
boolean useHeadersInDisplayName() default false;
@API(status = STABLE, since = "5.10")
char quoteCharacter() default '\'';
char delimiter() default '\0';
String delimiterString() default "";
String emptyValue() default "";
String[] nullValues() default {};
@API(status = STABLE, since = "5.10")
int maxCharsPerColumn() default 4096;
@API(status = STABLE, since = "5.10")
boolean ignoreLeadingAndTrailingWhitespace() default true;
@API(status = EXPERIMENTAL, since = "6.0.1")
char commentCharacter() default '#';
}
Example
package com.logicbig.example;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ParameterizedCsvSourceTest {
@ParameterizedTest(name = "#{index} length of '{0}' is {1}")
@CsvSource({
"car, 3",
"level, 5",
"radar, 5"
})
void lengthMatches(String input, int expectedLength) {
assertEquals(expectedLength, input.length());
}
@ParameterizedTest(name = "#{index} '{0}' palindrome? {1}")
@CsvSource({
"madam, true",
"hello, false",
"racecar, true"
})
void palindromeCheck(String input, boolean expected) {
assertEquals(expected, new StringBuilder(input).reverse().toString().equals(input));
}
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"apple|5|true",
"banana|6|false",
"cherry|6|true"
})
void testFruitDetails(String fruit, int length, boolean isTasty) {
// Test logic here
assertTrue(true);
}
}
mvn test -Dtest=ParameterizedCsvSourceTest OutputD:\example-projects\junit-5\junit-5-parameterized-tests\junit-5-parameterized-csvsource>mvn test -Dtest=ParameterizedCsvSourceTest [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-parameterized-csvsource >--------- [INFO] Building junit-5-parameterized-csvsource 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-parameterized-csvsource --- [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-parameterized-tests\junit-5-parameterized-csvsource\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-parameterized-csvsource --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-parameterized-csvsource --- [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-parameterized-tests\junit-5-parameterized-csvsource\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-parameterized-csvsource --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-parameterized-csvsource --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.ParameterizedCsvSourceTest - 0.398 ss [INFO] | +-- [OK] testFruitDetails(String, int, boolean)[1] "apple", "5", "true" - 0.122 ss [INFO] | +-- [OK] testFruitDetails(String, int, boolean)[2] "banana", "6", "false" - 0.004 ss [INFO] | +-- [OK] testFruitDetails(String, int, boolean)[3] "cherry", "6", "true" - 0 ss [INFO] | +-- [OK] lengthMatches(String, int) #1 length of {0} is "3" - 0.010 ss [INFO] | +-- [OK] lengthMatches(String, int) #2 length of {0} is "5" - 0.004 ss [INFO] | +-- [OK] lengthMatches(String, int) #3 length of {0} is "5" - 0 ss [INFO] | +-- [OK] palindromeCheck(String, boolean) #1 {0} palindrome? "true" - 0.007 ss [INFO] | +-- [OK] palindromeCheck(String, boolean) #2 {0} palindrome? "false" - 0.003 ss [INFO] | '-- [OK] palindromeCheck(String, boolean) #3 {0} palindrome? "true" - 0.002 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 9, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.852 s [INFO] Finished at: 2025-12-04T13:56:44+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- junit-jupiter-params 6.0.1 (Module "junit-jupiter-params" of JUnit)
- JDK 25
- Maven 3.9.11
|
|