0
0
JUnittesting~10 mins

@ArgumentsSource with custom providers 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 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'
ABeforeEach
BTest
CArgumentsSource
DDisplayName
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @ArgumentsSource
Using annotations from JUnit 4 instead of JUnit Jupiter
2fill in blank
medium

Complete 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'
ACustomProvider
BArgumentsSource
CTest
DValueSource
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the annotation name instead of the provider class
Omitting the .class suffix
3fill in blank
hard

Fix 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'
AgetArguments
BprovideArguments
Carguments
Dprovide
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name like getArguments
Missing the ExtensionContext parameter
4fill in blank
hard

Fill 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'
ACustomProvider
BString
Cint
DArguments
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type like int
Using Arguments type instead of the actual argument type
5fill in blank
hard

Fill 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'
AprovideArguments
B5
C10
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name
Providing arguments of wrong type
Using variable names instead of values in Arguments.of()