Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

When would you use @MethodSource or a custom ArgumentsProvider instead of @CsvSource, and what are the risks of large parameterized tests?

We use @MethodSource when the arguments are complex objects, such as an Order with lines and a customer, that do not fit in a CSV string; a static factory method returns a Stream of Arguments. A custom ArgumentsProvider does the same job in its own class, so several test classes can reuse it. The risk of large parameterized tests is that a big table hides which case matters and makes failures hard to read, so we keep display names meaningful, split cases that test different rules, and remember that recent JUnit versions can also parameterize a whole class.

Why this question is asked

@CsvSource is perfect for strings and numbers, but real business rules take real objects: an order, a set of permissions, a date range. Teams then reach for more powerful sources, and parameterized tests can grow into tables with hundreds of rows that nobody understands. Interviewers ask this to see whether we know the tools and also their cost. In this answer, we will see where @CsvSource stops being enough, write a @MethodSource and an ArgumentsProvider, look at what goes wrong with large tables, and see how to keep them readable, including the newer class-level parameterization.

1Where @CsvSource Stops Being Enough

@CsvSource turns each column into a parameter by converting text: "42" into an int, "2026-01-31" into a LocalDate. That works for simple values. It breaks down when a case needs an object built from several parts, a collection, or an object with behaviour, such as a stub that throws. Encoding an order with three lines into one CSV string would need our own parsing, which is test code we would then have to trust.

The answer is to build the arguments in Java, where we have constructors, builders and the type checker.

2Supplying Objects With @MethodSource

@MethodSource names a factory method that returns a Stream, Iterable or array of arguments. For several parameters, each element is an Arguments object, created with Arguments.of(...). With the default lifecycle the factory must be static, and if we omit the method name, JUnit looks for a factory with the same name as the test.

Java
class DiscountTest {
static Stream<Arguments> orders() {
return Stream.of(
Arguments.of(order(line("book", "20.00")), "0.00"), // under 50: no discount
Arguments.of(order(line("book", "30.00"), line("pen", "25.00")), "5.50"), // 10 percent
Arguments.of(order(line("tv", "600.00")).withCustomer(gold()), "90.00") // gold: 15 percent
);
}
@ParameterizedTest(name = "[{index}] total {0} -> discount {1}")
@MethodSource("orders")
void calculatesDiscount(Order order, String expected) {
assertEquals(new BigDecimal(expected), calculator.discount(order));
}
}

Notice that the factory is plain Java, so we can use helper methods like order() and line() to keep each case on one line. The report uses the Order's toString() in the display name, so a good toString(), or a records-based model, makes failures readable.

3Reusing Arguments With an ArgumentsProvider

When several test classes need the same cases, for example every implementation of a pricing strategy must handle the same edge cases, a static method in one test class is awkward to share. An ArgumentsProvider is a class that implements one method, provideArguments, and we point tests at it with @ArgumentsSource.

Java
public class EdgeCaseOrders implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
// check your JUnit version: newer releases add an overload with ParameterDeclarations
return Stream.of(
Arguments.of(emptyOrder()),
Arguments.of(orderWithZeroPriceLine()),
Arguments.of(orderWithMaxQuantity())
);
}
}
class StandardPricingTest {
@ParameterizedTest @ArgumentsSource(EdgeCaseOrders.class)
void neverNegative(Order order) { assertTrue(standard.price(order).signum() >= 0); }
}
class PromoPricingTest {
@ParameterizedTest @ArgumentsSource(EdgeCaseOrders.class)
void neverNegative(Order order) { assertTrue(promo.price(order).signum() >= 0); }
}
Choosing an argument source
SourceArgumentsReuse
@CsvSourcesimple values converted from textwithin one test
@MethodSourceany objects, built in Javasame class, or Class#method
@ArgumentsSourceany objects, from a provider classacross classes and modules

A provider can also read its cases from a file or be combined with a custom annotation, which is how test libraries share data across modules. @MethodSource can also reference a factory in another class by its fully qualified name, such as "com.shop.TestData#orders", which is a lighter way to share.

4The Risks of Large Parameterized Tests

Parameterized tests are so easy to extend that they grow. A table of 200 rows looks thorough, but it has real costs. It hides which case matters: when a reviewer sees 200 rows, they cannot tell which three describe the important rules and which 197 are noise. Failures are hard to read: a report saying [137] Order@5e2de80c -> 12.40 failed tells us nothing about what rule broke. And different rules get mixed: one table testing both the minimum order threshold and the gold customer rate means a change to one rule touches the cases of the other.

So we keep display names meaningful, either with a description argument or with good toString() methods, and we split tables by rule: one parameterized test for thresholds, one for customer tiers, one for currency rounding. Each table then has a handful of cases that show the boundaries, for example 49.99, 50.00 and 50.01 for a threshold, instead of random values.

Java
@ParameterizedTest(name = "{0}") // the description is the display name
@MethodSource
void thresholdBoundaries(String description, BigDecimal total, BigDecimal expected) { }
static Stream<Arguments> thresholdBoundaries() {
return Stream.of(
Arguments.of("just under 50: no discount", new BigDecimal("49.99"), new BigDecimal("0.00")),
Arguments.of("exactly 50: discount starts", new BigDecimal("50.00"), new BigDecimal("5.00")));
}
// Report: "exactly 50: discount starts" failed

JUnit also offers Named.of("description", value) to attach a readable name to a single argument without adding a parameter.

5Parameterizing a Whole Class

Sometimes every test in a class should run against several configurations, for example each test against both the H2 and PostgreSQL dialects, or against two implementations of an interface. Recent JUnit versions add @ParameterizedClass, introduced in JUnit 5.13, which takes the same sources and runs all the tests in the class once per set of arguments, injected through the constructor or fields. It was introduced as an experimental API, so check your version before relying on it.

Before that feature, the usual answers were an abstract test class with one subclass per implementation, or @Nested classes. Both still work and are fine for two or three variants.

6Follow-up questions

Why must the @MethodSource factory be static?

Because with the default per-method lifecycle, JUnit needs the arguments before it creates the test instance for each invocation. With @TestInstance(Lifecycle.PER_CLASS) the instance exists first, so the factory may be an instance method.

What is the difference between Arguments.of and returning plain values?

If the test has a single parameter, the factory can return a Stream of plain values, such as Stream<String>. For two or more parameters, each invocation needs a group of values, and Arguments.of wraps them in order to match the method's parameters.

Can parameterized cases come from a file?

Yes. @CsvFileSource reads a CSV file from the classpath, and a custom ArgumentsProvider can read JSON or YAML. Files are useful for long tables shared with non-developers, but they move the cases away from the test, so the display names become even more important.

7Conclusion

In this answer, we saw that @MethodSource supplies complex objects from a factory method returning a Stream of Arguments, and that an ArgumentsProvider moves that factory into its own class so many tests can reuse it. We looked at the costs of large tables, hidden important cases, unreadable failures and mixed rules, and kept them under control with descriptive display names, boundary values and one table per rule. Finally, we saw that @ParameterizedClass in recent JUnit versions can parameterize a whole class.

What to say in the interviewAbout 80 seconds aloud

Open with

I reach for @MethodSource when the arguments are real objects that do not fit in a CSV string, like an order with several lines and a customer tier.

Then, if they want more

The factory method returns a Stream of Arguments built with Arguments.of, so I can use builders and helper methods in plain Java, and it must be static unless the class uses the per-class lifecycle.

When several test classes need the same cases, for example every pricing implementation must handle the same edge cases, I put them in a class that implements ArgumentsProvider and use @ArgumentsSource, which can live in a shared test library.

The risk with large parameterized tests is that a table of hundreds of rows hides which cases matter, failures show up as an index and an object hash that tell me nothing, and different rules get mixed in one table.

So I give every case a meaningful display name, through a description argument, Named.of or a good toString, I pick boundary values like 49.99, 50 and 50.01 rather than random data, and I split tables by rule.

And when every test in a class must run against several configurations, recent JUnit versions have @ParameterizedClass, though I would check the version since it started as experimental.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "When would you use @MethodSource or a custom ArgumentsProvider instead of @CsvSource...": https://scrivio-interview-library.netlify.app/java/junit/b125138601/