What if you could test null and empty inputs perfectly with just one line of code?
Why @NullAndEmptySource in JUnit? - Purpose & Use Cases
Imagine testing a method that processes user input strings. You have to check how it behaves when the input is null or empty. Doing this by hand means writing separate test cases for each scenario, running them one by one, and carefully checking results.
Manually writing and running tests for null and empty inputs is slow and easy to forget. You might miss a case or make a typo. It's tiring to repeat similar code again and again, increasing the chance of mistakes.
The @NullAndEmptySource annotation in JUnit automatically provides both null and empty values as inputs to a single test method. This means you write one test, and JUnit runs it twice: once with null and once with empty string. It saves time and avoids missing cases.
void testInput() {
testMethod(null);
testMethod("");
}@ParameterizedTest
@NullAndEmptySource
void testInput(String input) {
// test logic
}You can easily and reliably test how your code handles null and empty inputs without extra effort or risk of missing cases.
When validating user registration forms, you want to ensure your code gracefully handles empty or missing fields. Using @NullAndEmptySource helps you test these edge cases quickly and confidently.
Manual testing of null and empty inputs is repetitive and error-prone.
@NullAndEmptySource runs tests automatically with null and empty values.
This makes your tests simpler, faster, and more reliable.
Practice
@NullAndEmptySource in JUnit tests?Solution
Step 1: Understand the role of @NullAndEmptySource
@NullAndEmptySource is used to add null and empty values automatically to parameterized tests.Step 2: Identify its purpose in testing
This helps test how the code behaves with missing or empty inputs without manually adding those cases.Final Answer:
To automatically provide null and empty values as test inputs -> Option CQuick Check:
@NullAndEmptySource adds null and empty inputs [OK]
- Thinking it skips tests with null or empty inputs
- Confusing it with random data generators
- Assuming it filters out null or empty values
@NullAndEmptySource in a JUnit 5 parameterized test method?Solution
Step 1: Check annotation compatibility
@NullAndEmptySource must be used with @ParameterizedTest to supply parameters.Step 2: Verify method signature
The test method must accept a parameter to receive the null or empty value.Final Answer:
@ParameterizedTest @NullAndEmptySource void testMethod(String input) { ... } -> Option DQuick Check:
@NullAndEmptySource needs @ParameterizedTest and parameter [OK]
- Using @NullAndEmptySource with @Test instead of @ParameterizedTest
- Writing test methods without parameters
- Forgetting to annotate with @ParameterizedTest
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"hello", "world"})
void testStrings(String input) {
// test logic here
}Solution
Step 1: Count inputs from @NullAndEmptySource
@NullAndEmptySource adds two inputs: null and empty string ("").Step 2: Count inputs from @ValueSource
@ValueSource provides two strings: "hello" and "world".Step 3: Calculate total invocations
Total = 2 (null and empty) + 2 (hello, world) = 4 test runs.Final Answer:
4 -> Option AQuick Check:
2 null/empty + 2 values = 4 [OK]
- Counting only @ValueSource inputs
- Counting null and empty as one input
- Forgetting that @NullAndEmptySource adds two inputs
@NullAndEmptySource:
@ParameterizedTest
@NullAndEmptySource
void testInput() {
// test logic
}Solution
Step 1: Check method signature
The test method has no parameters, but @NullAndEmptySource provides inputs that must be accepted.Step 2: Understand parameterized test requirements
Parameterized test methods must have parameters matching the sources; missing parameter causes error.Final Answer:
Missing parameter in test method to receive input values -> Option BQuick Check:
Parameterized test needs method parameter [OK]
- Leaving test method parameterless with @NullAndEmptySource
- Confusing @Test and @ParameterizedTest annotations
- Thinking @NullAndEmptySource requires @ValueSource
Solution
Step 1: Identify annotations for null and empty
@NullAndEmptySource adds both null and empty string automatically.Step 2: Add specific string inputs
@ValueSource(strings = {"admin", "guest"}) adds the required specific strings.Step 3: Verify combined usage
Combining @NullAndEmptySource with @ValueSource covers all needed inputs in one test.Final Answer:
@ParameterizedTest @NullAndEmptySource @ValueSource(strings = {"admin", "guest"}) -> Option AQuick Check:
Use @NullAndEmptySource plus @ValueSource for all inputs [OK]
- Using only @EmptySource or @NullSource missing one input
- Trying to put null in @ValueSource strings (not allowed)
- Not combining annotations properly
Ready to prove it?
Pass the test and this pattern moves unknown
