Master JUnit 5 Parameterized Tests with @CsvSource: A Practical Guide
Learn how to set up and run JUnit 5 parameterized tests with @CsvSource, handle nulls, escape commas, and troubleshoot common pitfalls. A step‑by‑step guide with examples and validation tips.
17 Jan 2026, 16:29 UTC

Desired Outcome
By the end of this guide you will be able to write a single test method that runs multiple times with different input values supplied directly in the test code, validate that the parameters are correctly converted, and troubleshoot common problems that can cause tests to be skipped or fail.
Prerequisites
- Java 11 or newer (JUnit 5 supports Java 8+)
- A build tool that can pull Maven Central artifacts (Maven, Gradle, or Gradle‑Kotlin)
- JUnit Jupiter dependency (org.junit.jupiter:junit-jupiter)
- IDE or command‑line build tool configured to run JUnit Platform tests
Step‑by‑Step Setup
- Add the JUnit Jupiter dependency
For Maven:
For Gradle:<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.3</version> <scope>test</scope> </dependency>dependencies { testImplementation "org.junit.jupiter:junit-jupiter:5.9.3" } test { useJUnitPlatform() } - Create a test class
Use the@ParameterizedTestannotation on a method and supply data with@CsvSource. Static imports are optional but improve readability.import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; class CalculatorTest { @ParameterizedTest @CsvSource({ "1, 2, 3", "4, 5, 9", "\N, 2, 2" // \\N represents a null value }) void testAddition(int a, int b, int expectedSum) { Calculator calc = new Calculator(); assertEquals(expectedSum, calc.add(a, b)); } } - Run the tests
From the command line:mvn testorgradle test. In an IDE, right‑click the test class and select "Run".
Common Pitfalls & Best Practices
- Mixing JUnit 4 and JUnit 5 – Ensure all tests use the Jupiter API. Mixing
org.junit.Testwithorg.junit.jupiter.api.Testcan cause discovery issues. - Missing static imports – Without importing
@ParameterizedTestand@CsvSource, the compiler will treat them as unknown annotations. - Comma inside values – Enclose the value in double quotes and escape internal commas with a backslash:
"John,\ Doe", 42. - Whitespaces – Leading/trailing whitespace is trimmed automatically. If you need a literal space, wrap the token in quotes.
- Null values – Use the literal string
\Nto representnull. Plain empty strings are also treated asnullby default. - Abstract test classes – Parameterized tests in abstract classes are not instantiated. Place the method in a concrete subclass or use a nested test class.
Validating Your Configuration
After running the tests, you should see the following in the console:
- Three separate test invocations, each labeled with the CSV row index.
- No assertion failures if the
Calculator.addmethod works correctly. - For the row containing
\N, the first argument will benullin the test method.
If a test is skipped, check the following:
- Is the test class compiled? Look for
Compilation errorsin the build output. - Do you have the correct JUnit Platform provider? Maven users should ensure
org.junit.platform:junit-platform-console-standaloneis present if running from the command line. - Is the test method static? Non‑static methods require a test instance; JUnit 5 defaults to
PER_METHODbut can be overridden with@TestInstance(Lifecycle.PER_CLASS).
Recovery Options
- Enable verbose logging – Add
-Dorg.junit.platform.console.logging=INFOto the JVM arguments to see detailed test discovery logs. - Isolate the test – Remove other test classes temporarily to confirm that the failure is not due to a global configuration issue.
- Check the
pom.xmlorbuild.gradlefor conflicting JUnit dependencies; runmvn dependency:treeorgradle dependenciesto spot duplicates.
When Data Sets Grow
For large or dynamic data sets, move away from @CsvSource and use @MethodSource:
static Stream<Arguments> additionProvider() {
return Stream.of(
Arguments.of(1, 2, 3),
Arguments.of(4, 5, 9),
Arguments.of(null, 2, 2)
);
}
@ParameterizedTest
@MethodSource("additionProvider")
void testAddition(int a, int b, int expectedSum) { … }
This approach allows complex logic, reading from files, or generating random data.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.