JUnit 5 Parameterized Tests | Java Fresher Interview
Fast recall
Quick revision
Core answer
@Test) and supply data via a source: `@ValueSource` for a single array of literals, `@CsvSource` for input→expected rows, `@MethodSource` for complex objects, plus @EnumSource and @NullSource.Interview-ready guide
Interview answer
A parameterized test lets me run the same test logic against many different inputs, so instead of copy-pasting five almost-identical tests I write one. The key difference is that I annotate the method with ParameterizedTest instead of Test, and then I attach a source that supplies the data.
There are a few sources, and I pick based on the shape of my data. ValueSource is the simplest — it feeds a single array of literals, like a set of integers or strings, and the method takes one parameter. CsvSource is what I use for input and expected pairs; each row is comma-separated, so I can write something like the password abcd1234 maps to valid true, and JUnit even converts the types automatically. MethodSource is for when the arguments are complex objects or include nulls — I point it at a static factory method that returns a Stream of Arguments, and each Arguments dot of call becomes one test run. There are also EnumSource and NullSource for those specific cases.
Under the hood, JUnit runs the method body once per row of data, and each run shows up as its own entry in the report. I can customize that label with the name attribute so the report reads clearly.
The common mistakes are using the regular Test annotation by accident, in which case the parameters never get injected, and reaching for MethodSource when CsvSource would be simpler. The dependency is junit-jupiter-params, but Spring Boot's test starter already pulls it in.
JUnit 5 Parameterized Tests | Java Fresher Interview
Direct Answer
A parameterized test runs the same test logic against many inputs, so you write one method instead of copy-pasting. You mark it with @ParameterizedTest (not @Test) and supply data via a source: @ValueSource for a single array of literals, @CsvSource for input→expected rows, @MethodSource for complex objects, plus @EnumSource and @NullSource.
Deep Dive & Architecture
A parameterized test separates test logic from test data. JUnit discovers the method as a template, reads its argument source, converts each row to the declared parameter types, and invokes the method once per row. Each invocation is reported as a separate case.
For example, a palindrome rule can use @ValueSource(strings = {"level", "radar"}) with one String parameter. Input-and-expected pairs fit @CsvSource; complex objects fit @MethodSource; enums fit @EnumSource; and null or empty cases can use the matching source annotations. The method must use @ParameterizedTest, not @Test, and its parameters must match the supplied values. A readable name pattern makes a failing row easy to identify.
Code Example
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
// @ValueSource: one argument per run
@ParameterizedTest
@ValueSource(ints = {2, 4, 100, -8})
void evenNumbersAreEven(int n) {
assertTrue(n % 2 == 0);
}
// @CsvSource: input -> expected pairs
@ParameterizedTest(name = "{0} chars -> valid={1}")
@CsvSource({"abc, false", "abcd1234, true", "'', false"})
void validatesPasswordLength(String pw, boolean expected) {
assertEquals(expected, validator.isValid(pw));
}
// @MethodSource: complex objects
@ParameterizedTest
@MethodSource("orders")
void pricesOrders(Order order, double expected) {
assertEquals(expected, service.price(order));
}
static Stream<Arguments> orders() {
return Stream.of(Arguments.of(new Order(100.0, PREMIUM), 90.0),
Arguments.of(new Order(100.0, REGULAR), 100.0));
}
Common Mistakes & Pitfalls
- Using
@Test. A parameterized method needs@ParameterizedTest; with@Testthe parameters won't be injected. - Wrong source for the data.
@ValueSourceonly carries one argument; for input/expected pairs use@CsvSource. - Forgetting the dependency. Parameterized tests live in
junit-jupiter-params, whichspring-boot-starter-testalready includes. @MethodSourcefactory not static. It must bestatic(unless using@TestInstance(PER_CLASS)).
Check your understanding
Answer these without looking back at the article.
- What is JUnit 5 and how do you write a basic test?
- What are the most important JUnit 5 annotations?
- How do you use assertions in JUnit 5?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.