JUnit 5 parameterized tests let you run one test method several times with different arguments, so you write the logic once and feed it a table of inputs. You mark the method with @ParameterizedTest instead of @Test, then add a source annotation such as @ValueSource, @CsvSource or @MethodSource that supplies the arguments. Each set of arguments runs as its own test invocation and is reported separately.
Parameterized tests live in the junit-jupiter-params module. If you depend on the aggregate junit-jupiter artifact (the default in current Maven and Gradle setups), it is already on your classpath.
Add the aggregate dependency once. With Maven:
Use the latest 5.x or newer release your project supports; the annotations in this guide are the same in newer releases. Now a first test. It checks that a small helper recognises palindromes:
The runner calls acceptsPalindromes three times, once per string. If one fails, the other two still run and the report shows exactly which value broke. Parameterized test methods must not be private and must not return a value, the same rule as @Test.
@ValueSource accepts one array of literals of a single type: strings, ints, longs, doubles, booleans, chars, bytes, shorts, floats or classes. It supplies exactly one argument per invocation.
Related annotations add the edge cases that literals cannot express:
| Annotation | What it supplies |
|---|---|
@NullSource | A single null |
@EmptySource | An empty String, collection, Map or array, depending on the parameter type |
@NullAndEmptySource | Both of the above in one annotation |
@EnumSource | Constants of an enum, optionally filtered |
Stacking @NullAndEmptySource and @ValueSource on one method combines their arguments, which is the standard way to cover the "null, empty, blank" trio for String validators. @EnumSource also supports mode = EnumSource.Mode.EXCLUDE to run every constant except the listed ones.
When a test needs inputs and an expected result, @CsvSource keeps each row on one line:
JUnit converts each cell to the parameter type automatically for primitives, wrappers, enums, LocalDate and other common types. A few CSV details save time:
'Smith, Anna'.null; use '' for an empty string.nullValues attribute to map a word such as "NIL" to null.For larger tables, put the rows in src/test/resources and use @CsvFileSource(resources = "/prices.csv", numLinesToSkip = 1). That keeps test data out of the Java file and lets non-developers edit it.
Literals cannot create objects. For that, point @MethodSource at a static method that returns a Stream, Iterable or array of Arguments:
The factory method must be static unless the class uses @TestInstance(Lifecycle.PER_CLASS). If you omit the method name, JUnit looks for a static method with the same name as the test. For reusable providers shared by several classes, implement ArgumentsProvider and use @ArgumentsSource. @FieldSource (added in a later 5.x release) does the same from a static field.
By default each invocation is labelled with its index and arguments. Customise it with the name attribute, which supports placeholders like {index}, {0}, {1} and {arguments}:
Good names turn a failure report into documentation, which matters when a table has fifty rows. For Arguments, use Named.of("label", value) or Arguments.argumentSet("label", ...) when the object's toString is not helpful.
Implicit conversion handles most types. When you need a custom one, annotate the parameter with @ConvertWith and a TypeSafeArgumentConverter. For tests with many columns, accept an ArgumentsAccessor and read cells by index, or use @AggregateWith to build a domain object. Prefer a record built through @MethodSource for readability; reach for aggregators only when the data comes from CSV.
Yes. You can repeat or mix sources, such as @ValueSource with @NullSource; the arguments from all of them are added together, provided each supplies the same parameter shape.
Run the whole method, then rerun the one failed invocation from the test tree. The display name helps you find it, which is another reason to set name.
@ParameterizedTest the same as @RepeatedTest?No. @RepeatedTest runs the same inputs several times, while @ParameterizedTest runs different inputs. Use the first for flakiness checks and the second for data variation.
@BeforeEach run for each invocation?Yes. Each invocation is a separate test execution, so @BeforeEach and @AfterEach run around every one.
Use @ParameterizedTest plus a source annotation to run one test body with many inputs. @ValueSource, @NullAndEmptySource and @EnumSource cover simple values, @CsvSource covers input and expected pairs, and @MethodSource handles objects. Set the name attribute so every row of data is easy to read in a failure report, and keep each parameterized test focused on one behaviour.