Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The @MethodSource annotation requires the name of the factory method that provides the test data. Here, 'factoryMethod' is the correct method name.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not making the factory method static.
Returning a wrong type instead of Stream.
✗ Incorrect
The factory method name should match the one used in @MethodSource. 'provideStrings' is a common descriptive name for such methods.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a List or Collection instead of Stream.
Using wrong generic types in the return type.
✗ Incorrect
The factory method must return Stream to be compatible with @MethodSource for parameterized tests.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Annotating the factory method with @ParameterizedTest.
Using incorrect method names that don't match @MethodSource.
✗ Incorrect
The factory method should be annotated with @MethodSource if annotated at all, but typically the factory method is not annotated. The test method is annotated with @ParameterizedTest and references the factory method by name. Here, the blanks expect the annotation for the factory method and the factory method name. The correct annotation for the factory method is @MethodSource (if used), and the factory method name is 'provideNumbers'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter types with the factory method arguments.
Using wrong method name in @MethodSource.
✗ Incorrect
The @MethodSource annotation references the factory method 'provideData'. The test method parameters must match the types provided by the factory method: an int and a String.