0
0
JUnittesting~10 mins

@MethodSource for factory methods in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use @MethodSource to provide test data from a factory method.

JUnit
@ParameterizedTest
@MethodSource("[1]")
void testWithFactoryMethod(String input) {
    assertNotNull(input);
}
Drag options to blanks, or click blank then click option'
AfactoryMethod
BdataProvider
CtestData
DsourceMethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist or is misspelled.
Forgetting to put the method name as a string in @MethodSource.
2fill in blank
medium

Complete the factory method to return a Stream of arguments for the parameterized test.

JUnit
static Stream<Arguments> [1]() {
    return Stream.of(
        Arguments.of("apple"),
        Arguments.of("banana"),
        Arguments.of("cherry")
    );
}
Drag options to blanks, or click blank then click option'
AdataProvider
BfactoryMethod
CprovideStrings
DtestData
Attempts:
3 left
💡 Hint
Common Mistakes
Not making the factory method static.
Returning a wrong type instead of Stream.
3fill in blank
hard

Fix the error in the factory method return type to correctly supply arguments.

JUnit
static [1] provideData() {
    return Stream.of(
        Arguments.of(1, "one"),
        Arguments.of(2, "two")
    );
}
Drag options to blanks, or click blank then click option'
ACollection<Object>
BStream<String>
CList<Arguments>
DStream<Arguments>
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a List or Collection instead of Stream.
Using wrong generic types in the return type.
4fill in blank
hard

Fill both blanks to correctly annotate and name the factory method for parameterized tests.

JUnit
@[1]
static Stream<Arguments> [2]() {
    return Stream.of(Arguments.of(10), Arguments.of(20));
}
Drag options to blanks, or click blank then click option'
AMethodSource
BParameterizedTest
CprovideNumbers
DtestNumbers
Attempts:
3 left
💡 Hint
Common Mistakes
Annotating the factory method with @ParameterizedTest.
Using incorrect method names that don't match @MethodSource.
5fill in blank
hard

Fill all three blanks to complete a parameterized test using @MethodSource with a factory method returning multiple argument types.

JUnit
@ParameterizedTest
@MethodSource("[1]")
void testMultipleArgs([2] number, [3] word) {
    assertNotNull(word);
    assertTrue(number > 0);
}
Drag options to blanks, or click blank then click option'
AprovideData
Bint
CString
DtestData
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter types with the factory method arguments.
Using wrong method name in @MethodSource.