Challenge - 5 Problems
JUnit Null and Empty Source Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of @NullSource in JUnit 5
What is the main purpose of using the @NullSource annotation in JUnit 5 parameterized tests?
Attempts:
2 left
💡 Hint
Think about testing how your code behaves when it receives no object.
✗ Incorrect
@NullSource adds a single null argument to the parameterized test, allowing you to verify how your code handles null inputs safely.
🧠 Conceptual
intermediate1:30remaining
Effect of @EmptySource in JUnit 5
Which of the following inputs does the @EmptySource annotation provide to a parameterized test method?
Attempts:
2 left
💡 Hint
Consider what 'empty' means for different data types.
✗ Incorrect
@EmptySource provides empty values appropriate to the parameter type, such as "" for strings, empty lists for collections, or empty arrays.
❓ Predict Output
advanced2:00remaining
Output of Parameterized Test with @NullSource and @EmptySource
Consider this JUnit 5 parameterized test method. What will be the total number of test invocations executed?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.EmptySource; class SampleTest { @ParameterizedTest @NullSource @EmptySource void testWithNullAndEmpty(String input) { System.out.println("Input: '" + input + "'"); } }
Attempts:
2 left
💡 Hint
Count how many inputs @NullSource and @EmptySource provide for a String parameter.
✗ Incorrect
@NullSource provides one null input, and @EmptySource provides one empty string input for String parameters, so total 2 invocations.
❓ assertion
advanced1:30remaining
Assertion Behavior with @NullSource and @EmptySource
Given a parameterized test using @NullSource and @EmptySource on a String parameter, which assertion correctly verifies that the input is either null or empty?
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.EmptySource; class AssertionTest { @ParameterizedTest @NullSource @EmptySource void testInput(String input) { // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Remember that input can be null or empty string.
✗ Incorrect
Since input can be null or empty string, the assertion must allow both conditions.
🔧 Debug
expert2:00remaining
Debugging a Failing Parameterized Test with @NullSource and @EmptySource
A parameterized test uses @NullSource and @EmptySource on a method with a non-nullable primitive int parameter. What error will occur when running the test?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.EmptySource; class DebugTest { @ParameterizedTest @NullSource @EmptySource void testPrimitive(int number) { System.out.println(number); } }
Attempts:
2 left
💡 Hint
Consider how Java primitives handle null values.
✗ Incorrect
Primitive types like int cannot accept null values, so @NullSource causes a ParameterResolutionException.