0
0
JUnittesting~5 mins

@MethodSource for factory methods in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AStream<Arguments>
Bvoid
Cint
DString
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
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")
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
Which annotation is best for simple single-parameter values in JUnit parameterized tests?
A@CsvSource
B@MethodSource
C@ValueSource
D@Test
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.