What is the purpose of the @MethodSource annotation in JUnit?
It provides a way to supply test data from a factory method for parameterized tests.
Click to reveal answer
beginner
How should a factory method used with @MethodSource be defined?
It should be a static method that returns a Stream, Iterable, Iterator, or array of arguments.
Click to reveal answer
intermediate
Can a @MethodSource factory method be in a different class than the test class?
Yes, by specifying the fully qualified class name in the @MethodSource annotation.
Click to reveal answer
intermediate
What happens if the factory method specified in @MethodSource does not exist or is not static?
JUnit will fail to run the test and report an error indicating the method is missing or invalid.
Click to reveal answer
intermediate
Why use @MethodSource instead of @ValueSource for parameterized tests?
@MethodSource supports complex or multiple parameters and dynamic test data, while @ValueSource supports only simple literals.
Click to reveal answer
Which of the following is a valid return type for a factory method used with @MethodSource?
AStream<Arguments>
Bvoid
Cint
DString
✗ Incorrect
Factory methods must return a Stream, Iterable, Iterator, or array of arguments for @MethodSource.
How do you specify a factory method named 'dataProvider' in the same class for @MethodSource?
A@MethodSource(dataProvider())
B@MethodSource("dataProvider")
C@MethodSource(dataProvider)
D@MethodSource
✗ Incorrect
The method name is passed as a string to @MethodSource.
If the factory method is in another class named 'TestData', how do you reference it in @MethodSource?
A@MethodSource("TestData#methodName")
B@MethodSource(TestData.methodName)
C@MethodSource("methodName")
D@MethodSource("TestData.methodName")
✗ Incorrect
Use the syntax "ClassName#methodName" as a string to reference external factory methods.
What must be true about the factory method used with @MethodSource?
AIt must throw an exception
BIt must be private
CIt must return void
DIt must be static
✗ Incorrect
Factory methods must be static so JUnit can call them without an instance.
Which annotation is best for simple single-parameter values in JUnit parameterized tests?
A@CsvSource
B@MethodSource
C@ValueSource
D@Test
✗ Incorrect
@ValueSource is designed for simple literal values; @MethodSource is for complex or multiple parameters.
Explain how to create and use a factory method with @MethodSource in JUnit.
Think about how JUnit gets test data from a method.
You got /4 concepts.
Describe the advantages of using @MethodSource over @ValueSource in parameterized tests.
Consider the types and complexity of test data each supports.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @MethodSource in JUnit tests?
easy
A. To skip a test method during execution
B. To create a new test class automatically
C. To provide multiple sets of data to a single test method
D. To run tests in parallel threads
Solution
Step 1: Understand the role of @MethodSource
@MethodSource is used to supply multiple data sets to a single test method, allowing it to run multiple times with different inputs.
Step 2: Compare options with this role
Options B, C, and D describe unrelated features. Only To provide multiple sets of data to a single test method correctly describes the purpose of @MethodSource.
Final Answer:
To provide multiple sets of data to a single test method -> Option C
Quick Check:
@MethodSource = multiple data sets [OK]
Hint: Remember: @MethodSource feeds data to one test method [OK]
Confusing @MethodSource with test skipping annotations
Assuming @MethodSource controls test threading
2. Which of the following is the correct way to declare a factory method for @MethodSource in JUnit 5?
easy
A. public static Stream<Arguments> provideData()
B. private Stream<Arguments> provideData()
C. public Stream<Arguments> provideData()
D. static Stream<Arguments> provideData()
Solution
Step 1: Recall factory method requirements
Factory methods used with @MethodSource must be static and public to be accessible by the test framework.
Step 2: Check each option
public static Stream<Arguments> provideData() is public and static, matching requirements. private Stream<Arguments> provideData() is private, so inaccessible. public Stream<Arguments> provideData() is public but not static. static Stream<Arguments> provideData() is static but missing public modifier.
Final Answer:
public static Stream<Arguments> provideData() -> Option A
Quick Check:
Factory method = public static [OK]
Hint: Factory methods must be public and static [OK]
Common Mistakes:
Omitting static keyword
Making the method private
Using wrong return type
3. Given the following code snippet, what will be the output when the test runs?
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
import java.util.stream.Stream;
public class CalculatorTest {
@ParameterizedTest
@MethodSource("dataProvider")
void testAdd(int a, int b, int expected) {
assertEquals(expected, a + b);
}
static Stream<Arguments> dataProvider() {
return Stream.of(
Arguments.of(1, 2, 3),
Arguments.of(2, 3, 5),
Arguments.of(3, 5, 9)
);
}
}
medium
A. Test fails on the last data set because 3 + 5 != 9
B. All tests pass successfully
C. Compilation error due to missing imports
D. Runtime error because dataProvider is not static
C. The @MethodSource annotation should specify a class name
D. The Stream should contain Arguments objects, not Integers
Solution
Step 1: Check factory method signature
The method 'numbers' is not static, but factory methods for @MethodSource must be static to be accessible.
Step 2: Suggest fix
Make the 'numbers' method static by adding the static keyword.
Final Answer:
The factory method 'numbers' must be static -> Option B
Quick Check:
Factory method must be static [OK]
Hint: Factory methods must be static to be found [OK]
Common Mistakes:
Forgetting static keyword on factory method
Thinking test methods must return values
Assuming @MethodSource needs class name always
5. You want to test a method that calculates discounts for different customer types using @MethodSource. The factory method returns a Stream<Arguments> with customer type and expected discount. Which approach correctly handles a case where the discount is zero for unknown customer types without failing the test?
hard
A. Exclude unknown customer types from the factory method to avoid test failure
B. Use a separate test method without @MethodSource for unknown types
C. Throw an exception in the factory method for unknown customer types
D. Include an Arguments entry with unknown customer type and expected discount 0 in the factory method
Solution
Step 1: Understand test coverage needs
To test all cases including unknown customer types, the factory method should provide data for them explicitly.
Step 2: Choose approach that includes zero discount case
Including an Arguments entry with unknown type and expected discount 0 ensures the test runs and verifies correct behavior without failure.
Final Answer:
Include an Arguments entry with unknown customer type and expected discount 0 in the factory method -> Option D
Quick Check:
Test all cases by including zero discount data [OK]
Hint: Always include edge cases in factory data [OK]
Common Mistakes:
Skipping edge cases in factory method
Expecting exceptions instead of valid results
Splitting tests unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.