Challenge - 5 Problems
Custom Argument Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check what numbers the custom argument provider returns and what the assertion checks.
✗ Incorrect
The custom argument provider returns only even numbers (2, 4, 6, 8). The test asserts that each number is even using 'assertTrue(number % 2 == 0)'. Since all provided numbers are even, all assertions pass and the test succeeds.
❓ assertion
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check which numbers satisfy the condition number < 5.
✗ Incorrect
The numbers 1 and 3 are less than 5, so their assertions pass. The numbers 5 and 7 are not less than 5, so their assertions fail.
🔧 Debug
advanced2: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; } }
Attempts:
2 left
💡 Hint
Check what the provideArguments method returns and what JUnit expects.
✗ Incorrect
JUnit expects provideArguments to return a non-null Stream of Arguments. Returning null causes a NullPointerException at runtime.
❓ framework
advanced2: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?
Attempts:
2 left
💡 Hint
Custom argument providers require a specific annotation and test type.
✗ Incorrect
Custom argument providers must be used with @ParameterizedTest and @ArgumentsSource annotations. Option D correctly uses both. Option D uses @Test which is not for parameterized tests. Options C and D use built-in sources, not custom providers.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Consider how JUnit 5 manages instances of argument providers during test execution.
✗ Incorrect
JUnit 5 creates a new instance of the argument provider for each test method invocation, so any internal state like a counter will reset each time provideArguments is called.