Challenge - 5 Problems
NullAndEmptySource 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 @NullAndEmptySource with String parameter
Consider this JUnit 5 parameterized test using @NullAndEmptySource. What will be the total number of test invocations executed?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; class SampleTest { @ParameterizedTest @NullAndEmptySource void testWithNullAndEmpty(String input) { System.out.println(input == null ? "null" : input); } }
Attempts:
2 left
💡 Hint
Think about how many inputs @NullAndEmptySource provides to the test method.
✗ Incorrect
The @NullAndEmptySource annotation provides exactly two inputs to the test method: null and an empty string "". So the test runs twice.
❓ assertion
intermediate2:00remaining
Correct assertion for @NullAndEmptySource inputs
In a parameterized test using @NullAndEmptySource for a String parameter, which assertion correctly verifies that the input is either null or empty?
JUnit
import static org.junit.jupiter.api.Assertions.*; @ParameterizedTest @NullAndEmptySource void testInput(String input) { // Which assertion is correct here? }
Attempts:
2 left
💡 Hint
Remember @NullAndEmptySource provides null and empty string inputs.
✗ Incorrect
The inputs are either null or empty string "". The assertion must check for both conditions combined with OR.
🔧 Debug
advanced2:00remaining
Why does this @NullAndEmptySource test fail with a NullPointerException?
Given this test code, why does it throw a NullPointerException during execution?
JUnit
import static org.junit.jupiter.api.Assertions.*; @ParameterizedTest @NullAndEmptySource void testLength(String input) { assertTrue(input.length() == 0); }
Attempts:
2 left
💡 Hint
Think about what happens when input is null and you call a method on it.
✗ Incorrect
@NullAndEmptySource provides null and empty string. Calling input.length() when input is null causes a NullPointerException.
🧠 Conceptual
advanced2:00remaining
Behavior of @NullAndEmptySource combined with @ValueSource
What happens when you combine @NullAndEmptySource with @ValueSource(strings = {"test"}) in a parameterized test method with a String parameter?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.ValueSource; @ParameterizedTest @NullAndEmptySource @ValueSource(strings = {"test"}) void testCombined(String input) { System.out.println(input); }
Attempts:
2 left
💡 Hint
Think about how multiple sources combine in JUnit parameterized tests.
✗ Incorrect
JUnit combines all sources, so the test runs once for each input from both annotations: null, empty string, and "test".
❓ framework
expert3:00remaining
How does JUnit 5 internally handle @NullAndEmptySource for parameterized tests?
Which statement best describes how JUnit 5 processes the @NullAndEmptySource annotation when running a parameterized test?
Attempts:
2 left
💡 Hint
Consider how parameterized tests get their input values from annotations.
✗ Incorrect
@NullAndEmptySource is a built-in source that automatically supplies two inputs: null and empty string "" to the test method.