@ParameterizedTest JUnit 5 | Java Fresher Interview
Fast recall
Quick revision
Core answer
@ValueSource (single literals), @CsvSource (input→expected rows), @MethodSource (a Stream for complex objects), plus @EnumSource, @NullSource, and @CsvFileSource. It needs the junit-jupiter-params module.Interview-ready guide
Interview answer
A parameterized test runs the same test body against several argument sets. I annotate the method with @ParameterizedTest and attach a source.
@ValueSource supplies one simple value per invocation. @CsvSource is useful for a small input-and-expected table and applies supported text conversions. @MethodSource supplies typed or computed data from Java code; a local factory is static under the default PER_METHOD lifecycle, while PER_CLASS allows an instance factory. An external factory remains static. Other choices include enum, null, empty, and CSV-file sources.
Each argument set appears as a separate invocation in the report. The name attribute belongs on @ParameterizedTest and can include indexes or argument values. Use separate tests when cases no longer share the same behavior and assertion shape.
@ParameterizedTest JUnit 5 | Java Fresher Interview
Direct Answer
@ParameterizedTest runs the same test body once per input set, replacing copy-pasted near-identical tests. You feed data with a source: @ValueSource (single literals), @CsvSource (input→expected rows), @MethodSource (a Stream<Arguments> for complex objects), plus @EnumSource, @NullSource, and @CsvFileSource. It needs the junit-jupiter-params module.
Deep Dive & Architecture
@ParameterizedTest marks a reusable test template. An argument-source annotation supplies the values, and JUnit runs the template once for every supplied case. This keeps the behavior in one place while showing the set of examples clearly in the test report.
For example, @ValueSource(ints = {2, 4, 8}) is suitable for one integer parameter. Use @CsvSource when each case has several simple columns, @MethodSource for objects or computed data, @EnumSource for enum values, and @NullSource or @EmptySource for those edge cases. The source is part of the test design: choosing a complex factory for two simple values makes the test harder to read, while forcing objects into CSV strings makes it fragile.
Code Example
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import java.util.stream.Stream;
@ParameterizedTest
@ValueSource(strings = {"racecar", "level", "noon"})
void detectsPalindromes(String word) {
assertTrue(StringUtils.isPalindrome(word));
}
@ParameterizedTest
@CsvSource({"2, 3, 5", "0, 0, 0", "-1, 1, 0"})
void addsNumbers(int a, int b, int expected) {
assertEquals(expected, calc.add(a, b));
}
@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. Parameters won't be injected; you need@ParameterizedTest. - Wrong source.
@ValueSourcecarries only one argument; use@CsvSourcefor pairs. - Non-static
@MethodSource. Factory must bestaticunless using@TestInstance(PER_CLASS). - Missing module. Needs
junit-jupiter-params(already inspring-boot-starter-test).
Check your understanding
Answer these without looking back at the article.
- How do you use @CsvSource for testing multiple input-output pairs?
- How do you test boundary conditions using parameterized tests?
- How do you use @MethodSource for complex parameterized test arguments?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.