@MethodSource JUnit 5 | Java Fresher Interview
Fast recall
Quick revision
Core answer
PER_METHOD; PER_CLASS allows an instance factory. A factory in an external class must be static.Interview-ready guide
Interview answer
MethodSource is the parameterized source I use when the arguments are too rich for CSV. Instead of inline strings, I point it at a factory method that returns a Stream of Arguments, and each Arguments dot of call becomes one run of the test, with the elements mapped to the test's parameters.
The reason I'd choose it over CsvSource is when my inputs are real domain objects, computed values, or include nulls — things you can't cleanly express as comma-separated text. For example, testing a pricing service, my factory can produce a premium customer with a hundred dollar order expecting ninety, a regular customer expecting a hundred, and an edge case with a zero order. Each of those is an Arguments dot of with actual objects, which is much clearer than trying to encode them as strings.
There are a couple of rules. The factory method has to be static by default — if I want it non-static, I switch the class to the per-class test instance lifecycle. And if I leave the MethodSource value blank, it looks for a method with the same name as the test; otherwise I name it explicitly. A nice feature is that I can also reference a factory in a completely different class using its fully-qualified name with a hash before the method name, which is great for sharing test data across classes.
Common mistakes are forgetting the static requirement, getting the method name wrong, and reaching for MethodSource when a simple ValueSource or CsvSource would do.
@MethodSource JUnit 5 | Java Fresher Interview
Direct Answer
@MethodSource supplies arguments from a factory method. A multi-argument factory commonly returns Stream<Arguments>, while Jupiter also accepts other supported stream-like return types. Use it for typed objects, computed cases, or reusable data. A factory in the test class is normally static under PER_METHOD; PER_CLASS allows an instance factory. A factory in an external class must be static.
Deep Dive & Architecture
@MethodSource connects a parameterized test to a factory method that supplies argument sets. It is useful when inline annotations would make the data hard to read—for example, when each case contains an Order, a CustomerType, and an expected BigDecimal. The factory commonly returns Stream<Arguments>, although Jupiter supports several stream-like forms.
For example, static Stream<Arguments> discountCases() can return cases through Arguments.of(...), and @MethodSource("discountCases") feeds them to the test. If the annotation has no value, JUnit looks for a factory with the same name as the test method. A local factory is static under the default per-method lifecycle, while @TestInstance(PER_CLASS) allows it to be an instance method. A factory referenced from another class must remain static. Keep the data readable and deterministic.
Code Example
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import java.util.stream.Stream;
@ParameterizedTest
@MethodSource("discountScenarios")
void appliesDiscount(Customer customer, Order order, double expected) {
assertEquals(expected, pricingService.finalPrice(order, customer));
}
// Factory: static, returns Stream<Arguments>
static Stream<Arguments> discountScenarios() {
return Stream.of(
Arguments.of(new Customer(PREMIUM), new Order(100.0), 90.0),
Arguments.of(new Customer(REGULAR), new Order(100.0), 100.0),
Arguments.of(new Customer(PREMIUM), new Order(0.0), 0.0)
);
}
// Reference a factory in another class by fully-qualified name:
// @MethodSource("com.example.TestData#discountScenarios")
Common Mistakes & Pitfalls
- Non-static local factory under
PER_METHOD. Usestaticor select@TestInstance(PER_CLASS). External factory methods must be static. - Name mismatch. Blank
@MethodSourcedefaults to a method named like the test; otherwise name it explicitly. - Unnecessary complexity. For simple literals or pairs,
@ValueSource/@CsvSourceare often clearer. - Argument shape mismatch. Make each emitted item match the test method's parameter list; use
Arguments.of(...)for several parameters.
Check your understanding
Answer these without looking back at the article.
- What is @ParameterizedTest and what sources can you use?
- How do you use @CsvSource for testing multiple input-output pairs?
- How do you test boundary conditions using parameterized tests?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.