Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class that implements the ArgumentsProvider interface.
JUnit
public class MyArgumentsProvider implements [1] { @Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { return Stream.of(Arguments.of(1, 2), Arguments.of(3, 4)); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test annotation instead of implementing ArgumentsProvider.
Implementing the wrong interface like ArgumentsSource.
✗ Incorrect
The class must implement the ArgumentsProvider interface to provide custom arguments for parameterized tests.
2fill in blank
mediumComplete the code to annotate a parameterized test method to use the custom argument provider.
JUnit
@ParameterizedTest @[1](MyArgumentsProvider.class) void testWithCustomProvider(int a, int b) { assertTrue(a < b); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource or @CsvSource which are for built-in sources.
Using @Test instead of @ParameterizedTest.
✗ Incorrect
The @ArgumentsSource annotation is used to specify a custom ArgumentsProvider class for parameterized tests.
3fill in blank
hardFix the error in the method signature to correctly override provideArguments.
JUnit
@Override public Stream<? extends Arguments> [1](ExtensionContext context) { return Stream.of(Arguments.of("hello", 5)); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the method name as provideArgument or getArguments.
Using a different method name that does not override the interface.
✗ Incorrect
The method to override in ArgumentsProvider is provideArguments with an 's' at the end.
4fill in blank
hardFill both blanks to create a stream of arguments with a string and an integer.
JUnit
return Stream.of(Arguments.of([1], [2]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean or null instead of expected types.
Not quoting the string argument.
✗ Incorrect
Arguments.of takes the values to pass to the test method. Here a string "JUnit" and integer 5 are correct.
5fill in blank
hardFill all three blanks to create a parameterized test with a custom argument provider and assert the sum.
JUnit
@ParameterizedTest @ArgumentsSource([1].class) void testSum(int x, int y) { int sum = x + y; assertEquals([2], sum); } public static class [3] implements ArgumentsProvider { @Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { return Stream.of(Arguments.of(2, 3), Arguments.of(4, 5)); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching class names between annotation and class.
Using wrong expected sum values in assertion.
✗ Incorrect
The test uses MyArgumentsProvider as the source, asserts sum equals 5 for first pair, and the class name is MyArgumentsProvider.