Challenge - 5 Problems
JUnit ArgumentsSource 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 @ArgumentsSource with a custom provider
Given the following JUnit 5 test code using a custom ArgumentsProvider, 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.ArgumentsSource; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.ArgumentsProvider; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertTrue; class CustomProviderTest { static class EvenNumberProvider implements ArgumentsProvider { @Override public Stream<? extends Arguments> provideArguments(ExtensionContext context) { return Stream.of(2, 4, 6, 7).map(Arguments::of); } } @ParameterizedTest @ArgumentsSource(EvenNumberProvider.class) void testEvenNumbers(int number) { assertTrue(number % 2 == 0); } }
Attempts:
2 left
💡 Hint
Check which numbers are provided and what the assertion checks.
✗ Incorrect
The custom provider supplies numbers 2, 4, 6, and 7. The test asserts that each number is even. Since 7 is not even, the assertion fails causing the test to fail with an AssertionError.
❓ assertion
intermediate2:00remaining
Correct assertion for a parameterized test using @ArgumentsSource
You have a parameterized test using a custom ArgumentsProvider that supplies strings. Which assertion correctly verifies that each string is not empty?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; import static org.junit.jupiter.api.Assertions.*; class StringTest { @ParameterizedTest @ArgumentsSource(NonEmptyStringProvider.class) void testNonEmptyStrings(String input) { // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Check which assertion directly tests that the string is not empty.
✗ Incorrect
assertFalse(input.isEmpty()) directly checks that the string is not empty. assertTrue(input.length() > 0) also works but is less readable. assertNotNull only checks for null, not emptiness. assertEquals("", input) checks for empty string, which is opposite of the requirement.
🔧 Debug
advanced2:00remaining
Identify the cause of test failure with a custom ArgumentsProvider
A parameterized test using a custom ArgumentsProvider fails with the error: "java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer". What is the most likely cause?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; class NumberTest { @ParameterizedTest @ArgumentsSource(StringToIntegerProvider.class) void testNumber(Integer number) { // test logic } }
Attempts:
2 left
💡 Hint
Check the types returned by the provider and the test method parameter type.
✗ Incorrect
The ClassCastException indicates the provider returns String objects but the test method expects Integer parameters. This mismatch causes the failure.
❓ framework
advanced2:00remaining
Behavior of @ArgumentsSource with multiple providers
What happens if a parameterized test method is annotated with multiple @ArgumentsSource annotations providing different argument streams?
JUnit
@ParameterizedTest @ArgumentsSource(ProviderOne.class) @ArgumentsSource(ProviderTwo.class) void testMultipleSources(String input) { // test logic }
Attempts:
2 left
💡 Hint
Check JUnit 5 documentation on multiple @ArgumentsSource annotations on the same method.
✗ Incorrect
JUnit 5 does not support multiple @ArgumentsSource annotations on the same test method. This causes a runtime exception.
🧠 Conceptual
expert2:00remaining
Custom ArgumentsProvider lifecycle and state management
Consider a custom ArgumentsProvider that maintains internal state between calls to provideArguments. What is the recommended practice regarding state in such providers?
Attempts:
2 left
💡 Hint
Think about how JUnit creates and uses ArgumentsProvider instances during test execution.
✗ Incorrect
JUnit may create multiple instances of ArgumentsProvider, so relying on internal mutable state is unsafe. Providers should be stateless or use external mechanisms for state.