0
0
JUnittesting~20 mins

Custom argument providers in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Argument Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a JUnit test using a custom argument provider
Given the following JUnit 5 test code using a custom argument provider, what will be the output when the test runs?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CustomArgProviderTest {
    static class EvenNumberProvider implements ArgumentsProvider {
        @Override
        public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
            return Stream.of(2, 4, 6, 8).map(Arguments::of);
        }
    }

    @ParameterizedTest
    @ArgumentsSource(EvenNumberProvider.class)
    void testEvenNumbers(int number) {
        assertTrue(number % 2 == 0);
    }
}
AAll tests pass successfully
BTest fails because the argument provider returns odd numbers
CTest fails due to a runtime exception in the argument provider
DTest fails because the test method signature is incorrect
Attempts:
2 left
💡 Hint
Check what numbers the custom argument provider returns and what the assertion checks.
assertion
intermediate
2:00remaining
Assertion outcome with a custom argument provider
Consider a custom argument provider that supplies the following integers: 1, 3, 5, 7. The test method asserts that the number is less than 5. What will be the assertion outcomes for each input?
JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LessThanFiveProviderTest {
    static class OddNumberProvider implements ArgumentsProvider {
        @Override
        public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
            return Stream.of(1, 3, 5, 7).map(Arguments::of);
        }
    }

    @ParameterizedTest
    @ArgumentsSource(OddNumberProvider.class)
    void testLessThanFive(int number) {
        assertTrue(number < 5);
    }
}
ATests for 1, 3, and 5 pass; test for 7 fails
BTests for 1 and 3 pass; tests for 5 and 7 fail
CAll tests fail
DAll tests pass
Attempts:
2 left
💡 Hint
Check which numbers satisfy the condition number < 5.
🔧 Debug
advanced
2:00remaining
Identify the error in a custom argument provider implementation
What is the cause of the runtime error in the following custom argument provider code?
JUnit
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.stream.Stream;

public class NullProvider implements ArgumentsProvider {
    @Override
    public Stream<Arguments> provideArguments(org.junit.jupiter.api.extension.ExtensionContext context) {
        return null;
    }
}
AThe provideArguments method returns null instead of a Stream, causing a NullPointerException
BThe class does not implement ArgumentsProvider correctly
CThe method signature is incorrect
DThe Stream returned contains null elements
Attempts:
2 left
💡 Hint
Check what the provideArguments method returns and what JUnit expects.
framework
advanced
2:00remaining
Correct usage of custom argument provider in JUnit 5
Which of the following is the correct way to use a custom argument provider in a JUnit 5 parameterized test?
A
@ParameterizedTest
@CsvSource({"a", "b"})
void testMethod(String input) { /* test code */ }
B
@Test
@ArgumentsSource(MyProvider.class)
void testMethod(String input) { /* test code */ }
C
@ParameterizedTest
@ValueSource(strings = {"a", "b"})
void testMethod(String input) { /* test code */ }
D
@ParameterizedTest
@ArgumentsSource(MyProvider.class)
void testMethod(String input) { /* test code */ }
Attempts:
2 left
💡 Hint
Custom argument providers require a specific annotation and test type.
🧠 Conceptual
expert
3:00remaining
Understanding lifecycle and state in custom argument providers
A custom argument provider maintains internal state by storing a counter that increments each time provideArguments is called. What is the expected behavior of this counter across multiple test method invocations in JUnit 5?
AThe argument provider is a singleton and the counter is shared across all test classes
BThe same instance is reused for all test methods, so the counter accumulates across all tests
CA new instance of the argument provider is created for each test method invocation, so the counter resets each time
DJUnit 5 does not allow stateful argument providers and throws an error
Attempts:
2 left
💡 Hint
Consider how JUnit 5 manages instances of argument providers during test execution.