Replace duplicated JUnit tests with @ParameterizedTest
Use JUnit Jupiter parameterized tests to express one validation rule across many inputs instead of repeating nearly identical test methods.
05 Aug 2026, 01:58 UTC

The problem: one rule, many test methods
Imagine your team validates product SKUs with a rule like "three uppercase letters, a hyphen, four digits." You write one test for a valid SKU, then another for lowercase letters, another for too few digits, and another for a null input. Each method repeats the same assertion; only the input and expected result change. When the validation rule changes, you must edit every method. A new edge case means another copied method. The shared behavior becomes hard to see because it is buried under repetition.
The useful takeaway: JUnit Jupiter's @ParameterizedTest lets you write the assertion once. @CsvSource supplies the input and expected-result pairs as data rows. Each row runs as a separate invocation, so a failure points to the exact case that broke.
How the feature works
@ParameterizedTest marks a JUnit Jupiter test method as data-driven instead of running once. @CsvSource provides comma-separated values that JUnit converts to the parameter types declared by your test method. A row like "ABC-1234, true" can bind to a String sku parameter and a boolean expected parameter. You need the junit-jupiter-params module on the test classpath; without it, JUnit may fail to discover or run the tests.
Worked example: SKU validation
Here is illustrative production code. It is not part of JUnit; adapt it to your own validation contract.
final class SkuRules {
private SkuRules() {
}
static boolean isValidSku(String sku) {
return sku != null && sku.matches("[A-Z]{3}-[0-9]{4}");
}
}
Instead of separate methods, write one parameterized test:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SkuRulesTest {
@ParameterizedTest(name = "{index} => sku={0}, expected={1}")
@CsvSource({
"ABC-1234, true",
"abc-1234, false",
"ABC-12, false",
"null, false"
})
void validatesSku(String sku, Boolean expected) {
assertEquals(expected, SkuRules.isValidSku(sku));
}
}
JUnit reports each CSV row as its own invocation. If ABC-12 fails, the failure label includes that row rather than a generic method name. Note the Boolean wrapper type: @CsvSource converts the literal null to a Java null, which binds to Boolean but not to primitive boolean. Conversion and quoting details vary by JUnit version, so check your project's user guide.
Running and verifying the conversion
Add the parameterized-test module if it is missing. For Maven, use a dependency like this, substituting your project's JUnit version:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
Run the suite from the project root with normal permissions for your build user:
./mvnw test -Dtest=SkuRulesTest
Expected check: the run should report four invocations for one method, not four separate methods. To verify failure diagnostics, temporarily change one expected value—for example, mark abc-1234 as true—run again, confirm the failure identifies that row, then restore the value. Compare executed test counts before and after conversion to confirm equivalent coverage. Risk: without junit-jupiter-params, the build may fail or silently execute zero parameterized invocations.
When not to parameterize
- Different setup paths. If rows require different mocks, database rows, or file fixtures, a single method can hide branching logic and make setup harder to follow.
- Shared mutable state. Each row should be independent. Avoid static or instance fields that one row mutates and another reads.
- Complex types.
@CsvSourcefits compact, homogeneous cases. For objects, collections, or programmatic construction, use@MethodSourceinstead. - Distinct behaviors. If two rows test different rules, keep them in separate test methods with descriptive names.
CSV quoting, empty values, and null handling have version-sensitive details. Consult the JUnit user guide for your release before relying on edge-case conversions.
Adoption checklist
- Find one cluster of tests that differ only by input and expected output.
- List the input and expected-result pairs.
- Extract the common assertion into a
@ParameterizedTest. - Add the pairs to
@CsvSource, or switch to@MethodSourceif the data is complex. - Run the suite before and after, and confirm invocation counts and coverage match.
Parameterized tests are a data organization tool, not a replacement for thoughtful test design. Start with one duplicated cluster, verify the reported invocations, and expand only where the rows genuinely share one behavior.