These annotations help test methods run with special input values like null or empty data. This makes sure your code handles these cases well.
@NullSource and @EmptySource in JUnit
@NullSource
@EmptySource
void testMethod(Type input) { ... }@NullSource provides a single null value as input.
@EmptySource provides empty values like "" for strings, empty collections, or arrays depending on the parameter type.
@ParameterizedTest @NullSource void testWithNull(String input) { assertNull(input); }
@ParameterizedTest @EmptySource void testWithEmpty(String input) { assertEquals("", input); }
@ParameterizedTest @NullSource @EmptySource void testWithNullAndEmpty(String input) { assertTrue(input == null || input.isEmpty()); }
This test method runs twice: once with null and once with empty string. It checks that the input is either null or empty as expected.
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.EmptySource; import static org.junit.jupiter.api.Assertions.*; public class ExampleTest { @ParameterizedTest @NullSource @EmptySource void testNullAndEmptyInputs(String input) { if (input == null) { assertNull(input); } else { assertEquals("", input); } } }
You can combine @NullSource and @EmptySource to test both null and empty inputs easily.
@EmptySource works with strings, collections, arrays, and maps by providing their empty versions.
These annotations reduce repetitive code and improve test clarity.
@NullSource provides null input for parameterized tests.
@EmptySource provides empty input like empty string or collection.
Use them together to test edge cases simply and clearly.
Practice
@NullSource annotation in JUnit parameterized tests?Solution
Step 1: Understand
@NullSourcerole@NullSourceis used to supply a null value as input to a parameterized test method.Step 2: Differentiate from other sources
@EmptySourceprovides empty values like empty strings or collections, not null. Other options do not match@NullSourcebehavior.Final Answer:
To provide a null value as input to the test method parameter -> Option AQuick Check:
@NullSource= null input [OK]
- Confusing @NullSource with @EmptySource
- Thinking @NullSource provides empty strings
- Assuming @NullSource skips tests
@NullSource and @EmptySource together in a JUnit 5 parameterized test?Solution
Step 1: Check annotation placement
Both@NullSourceand@EmptySourcemust be placed above the test method along with@ParameterizedTest.Step 2: Verify method signature
The test method must accept a parameter matching the input type (e.g.,String input) to receive values from sources.Final Answer:
@ParameterizedTest @NullSource @EmptySource void testMethod(String input) { ... } -> Option CQuick Check:
Annotations above method with parameter = correct [OK]
- Placing @EmptySource inside method parameters
- Missing parameter in test method
- Wrong order or missing @ParameterizedTest
@ParameterizedTest
@NullSource
@EmptySource
void testInput(String input) {
System.out.println(input == null ? "null" : "'" + input + "'");
}Solution
Step 1: Identify inputs from annotations
@NullSourceprovides a null input, and@EmptySourceprovides an empty string input.Step 2: Understand output behavior
The method prints "null" if input is null, otherwise prints the input in quotes. So outputs will be "null" and "''" for empty string.Final Answer:
null and an empty string printed as: null and '' -> Option BQuick Check:
NullSource + EmptySource = null + "" [OK]
- Assuming EmptySource provides null
- Expecting extra inputs like spaces
- Ignoring null input from NullSource
@ParameterizedTest
@NullSource
@EmptySource
void test(String input) {
assertNotNull(input);
}What will happen when this test runs?
Solution
Step 1: Analyze inputs from sources
@NullSourceprovides a null input, and@EmptySourceprovides an empty string input.Step 2: Check assertion behavior
assertNotNull(input)will fail when input is null, causing an AssertionError.Final Answer:
Test fails with AssertionError due to null input -> Option AQuick Check:
assertNotNull fails on null input [OK]
- Thinking test passes because empty string is valid
- Assuming test skips null input
- Confusing compilation errors with runtime failures
Solution
Step 1: Identify needed inputs
You need null (from@NullSource), empty string (from@EmptySource), and a non-empty string (from@ValueSource).Step 2: Verify method signature and annotations
Method must accept a parameter to receive inputs. @ParameterizedTest @NullSource @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... } includes all three annotations and a parameter.Final Answer:
@ParameterizedTest @NullSource @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... } -> Option DQuick Check:
Combine NullSource, EmptySource, and ValueSource for all cases [OK]
- Omitting NullSource or EmptySource
- Missing method parameter
- Using annotations without @ParameterizedTest
Ready to prove it?
Pass the test and this pattern moves unknown
