Discover how a few simple annotations can save you hours of tedious testing work!
Why @NullSource and @EmptySource in JUnit? - Purpose & Use Cases
Imagine testing a method that processes user input. You have to check how it behaves when given null or empty values. Doing this by hand means writing separate test cases for each scenario, running them one by one, and carefully watching for mistakes.
Manually writing tests for null and empty inputs is slow and repetitive. It's easy to forget a case or make typos. Running tests separately wastes time and makes your test suite bulky and hard to maintain.
The @NullSource and @EmptySource annotations in JUnit let you automatically supply null and empty values to your parameterized tests. This means one test method can check many edge cases quickly and cleanly, reducing errors and saving time.
void testInput() {
testMethod(null);
testMethod("");
testMethod("data");
}@ParameterizedTest
@NullSource
@EmptySource
@ValueSource(strings = {"data"})
void testInput(String input) {
testMethod(input);
}It enables fast, clear, and reliable testing of edge cases like null and empty inputs without extra code clutter.
When testing a login form, you want to ensure the system handles empty usernames or passwords gracefully. Using @NullSource and @EmptySource lets you cover these cases effortlessly in one test.
Manual tests for null and empty inputs are slow and error-prone.
@NullSource and @EmptySource automate supplying these edge cases.
This leads to cleaner, faster, and more reliable tests.
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
