0
0
JUnittesting~10 mins

Custom argument 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 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'
AExtensionContext
BTest
CArgumentsSource
DArgumentsProvider
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test annotation instead of implementing ArgumentsProvider.
Implementing the wrong interface like ArgumentsSource.
2fill in blank
medium

Complete 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'
AValueSource
BCsvSource
CArgumentsSource
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource or @CsvSource which are for built-in sources.
Using @Test instead of @ParameterizedTest.
3fill in blank
hard

Fix 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'
AprovideArguments
BprovideArgument
CgetArguments
DargumentsProvider
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.
4fill in blank
hard

Fill 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'
A"JUnit"
B5
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean or null instead of expected types.
Not quoting the string argument.
5fill in blank
hard

Fill 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'
ASumArgumentsProvider
B5
CMyArgumentsProvider
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching class names between annotation and class.
Using wrong expected sum values in assertion.