Recall & Review
beginner
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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?
✗ 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.