Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the correct JUnit annotation for parameterized tests.
JUnit
import org.junit.jupiter.params.provider.[1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @ArgumentsSource
Using annotations from JUnit 4 instead of JUnit Jupiter
✗ Incorrect
The @ArgumentsSource annotation is imported from org.junit.jupiter.params.provider package to provide custom argument sources.
2fill in blank
mediumComplete the code to declare a parameterized test method using a custom ArgumentsProvider class named CustomProvider.
JUnit
@ParameterizedTest @ArgumentsSource([1].class) void testWithCustomProvider(String input) { // test code here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the annotation name instead of the provider class
Omitting the .class suffix
✗ Incorrect
The @ArgumentsSource annotation requires the class of the custom provider to supply arguments for the test method.
3fill in blank
hardFix the error in the custom ArgumentsProvider class by completing the method signature correctly.
JUnit
public class CustomProvider implements ArgumentsProvider { @Override public Stream<Arguments> [1](ExtensionContext context) { return Stream.of(Arguments.of("apple"), Arguments.of("banana")); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like getArguments
Missing the ExtensionContext parameter
✗ Incorrect
The method to override in ArgumentsProvider interface is provideArguments with ExtensionContext parameter.
4fill in blank
hardFill both blanks to create a parameterized test method that uses the custom provider and asserts the input is not null.
JUnit
@ParameterizedTest @ArgumentsSource([1].class) void testInputIsNotNull([2] input) { assertNotNull(input); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type like int
Using Arguments type instead of the actual argument type
✗ Incorrect
The test uses CustomProvider as the argument source and the input parameter type is String to match provided arguments.
5fill in blank
hardFill all three blanks to implement a custom ArgumentsProvider that provides two integer arguments and use it in a parameterized test method.
JUnit
public class IntProvider implements ArgumentsProvider { @Override public Stream<Arguments> [1](ExtensionContext context) { return Stream.of(Arguments.of([2]), Arguments.of([3])); } } @ParameterizedTest @ArgumentsSource(IntProvider.class) void testWithInts(int number) { assertTrue(number > 0); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name
Providing arguments of wrong type
Using variable names instead of values in Arguments.of()
✗ Incorrect
The method provideArguments returns a stream of Arguments with integer values 5 and 10 to be used in the test method.