0
0
JUnittesting~20 mins

@ArgumentsSource with custom providers in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit ArgumentsSource 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 @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);
    }
}
ATest does not run due to compilation error
BTest passes for all inputs
CTest fails with NullPointerException
DTest fails with AssertionError on input 7
Attempts:
2 left
💡 Hint
Check which numbers are provided and what the assertion checks.
assertion
intermediate
2: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?
    }
}
AassertFalse(input.isEmpty());
BassertEquals("", input);
CassertNotNull(input);
DassertTrue(input.length() > 0);
Attempts:
2 left
💡 Hint
Check which assertion directly tests that the string is not empty.
🔧 Debug
advanced
2: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
    }
}
AThe ArgumentsProvider returns Strings but the test expects Integers
BThe test method parameter type is incorrect and should be String
CThe ArgumentsProvider is missing the @Override annotation
DThe test method is missing the @Test annotation
Attempts:
2 left
💡 Hint
Check the types returned by the provider and the test method parameter type.
framework
advanced
2: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
}
AJUnit runs the test only with arguments from the first provider
BJUnit combines all arguments from both providers and runs the test for each argument
CJUnit throws a runtime exception due to multiple @ArgumentsSource annotations
DJUnit runs the test only with arguments from the last provider
Attempts:
2 left
💡 Hint
Check JUnit 5 documentation on multiple @ArgumentsSource annotations on the same method.
🧠 Conceptual
expert
2: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?
AArgumentsProvider should maintain mutable state to track test progress
BArgumentsProvider instances should be stateless because JUnit may create multiple instances
CArgumentsProvider must be a singleton to preserve state across tests
DArgumentsProvider should store state in static fields to share across tests
Attempts:
2 left
💡 Hint
Think about how JUnit creates and uses ArgumentsProvider instances during test execution.