It provides a single null argument to a parameterized test method, allowing you to test how your code handles null inputs.
Click to reveal answer
beginner
What is the purpose of the @EmptySource annotation in JUnit?
It supplies an empty value (like an empty string, empty collection, or empty array) as an argument to a parameterized test method to test how code behaves with empty inputs.
Click to reveal answer
intermediate
Can @NullSource and @EmptySource be used together in a single test method?
Yes, you can combine @NullSource and @EmptySource to run a parameterized test with both null and empty values as inputs, helping to cover edge cases.
Click to reveal answer
beginner
Example usage: How do you write a JUnit test method that tests a String parameter with null and empty values?
Use @ParameterizedTest with @NullSource and @EmptySource annotations above the test method. For example:
@ParameterizedTest
@NullSource
@EmptySource
void testWithNullAndEmpty(String input) {
// test logic here
}
Click to reveal answer
beginner
Why is it important to test with @NullSource and @EmptySource?
Because null and empty inputs are common edge cases that can cause bugs if not handled properly. Testing these helps ensure your code is robust and error-free.
Click to reveal answer
What input does @NullSource provide to a parameterized test?
AA zero integer
BAn empty string
CA null value
DA random value
✗ Incorrect
@NullSource provides a null value as input to test how the method handles null.
Which annotation supplies an empty collection or empty string to a test method?
A@NullSource
B@ValueSource
C@CsvSource
D@EmptySource
✗ Incorrect
@EmptySource provides empty values like empty strings or collections to test empty input handling.
Can @NullSource be used without @ParameterizedTest?
ANo, it must be used with @ParameterizedTest
BOnly with @RepeatedTest
COnly with @Test
DYes, always
✗ Incorrect
@NullSource works only with @ParameterizedTest to supply null arguments.
What happens if you combine @NullSource and @EmptySource on a test method?
AThe test runs only once with null
BThe test runs twice: once with null, once with empty value
CThe test runs only once with empty value
DThe test fails to run
✗ Incorrect
Combining both runs the test twice, once for each input, covering both edge cases.
Which of these is NOT a typical use of @EmptySource?
ATesting null values
BTesting empty strings
CTesting empty lists
DTesting empty arrays
✗ Incorrect
@EmptySource does not provide null values; @NullSource is used for null.
Explain how @NullSource and @EmptySource help improve test coverage in JUnit parameterized tests.
Think about why testing null and empty inputs is important.
You got /4 concepts.
Describe how to write a JUnit test method that uses both @NullSource and @EmptySource annotations.
Remember the syntax and purpose of these annotations.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @NullSource annotation in JUnit parameterized tests?
easy
A. To provide a null value as input to the test method parameter
B. To provide an empty string as input to the test method parameter
C. To provide multiple non-null values to the test method
D. To skip the test when the input is null
Solution
Step 1: Understand @NullSource role
@NullSource is used to supply a null value as input to a parameterized test method.
Step 2: Differentiate from other sources
@EmptySource provides empty values like empty strings or collections, not null. Other options do not match @NullSource behavior.
Final Answer:
To provide a null value as input to the test method parameter -> Option A
Quick Check:
@NullSource = null input [OK]
Hint: NullSource always means null input for tests [OK]
Common Mistakes:
Confusing @NullSource with @EmptySource
Thinking @NullSource provides empty strings
Assuming @NullSource skips tests
2. Which of the following is the correct way to use both @NullSource and @EmptySource together in a JUnit 5 parameterized test?
easy
A. @ParameterizedTest
@NullSource
void testMethod(@EmptySource String input) { ... }
B. @ParameterizedTest
@EmptySource
@NullSource
void testMethod() { ... }
C. @ParameterizedTest
@NullSource
@EmptySource
void testMethod(String input) { ... }
D. @ParameterizedTest
@NullSource
@EmptySource
void testMethod() { ... }
Solution
Step 1: Check annotation placement
Both @NullSource and @EmptySource must 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.
A. Test fails with AssertionError due to null input
B. Test passes for all inputs
C. Compilation error because of missing imports
D. Test is skipped because of null input
Solution
Step 1: Analyze inputs from sources
@NullSource provides a null input, and @EmptySource provides 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 A
Quick Check:
assertNotNull fails on null input [OK]
Hint: assertNotNull fails if input is null from NullSource [OK]
Common Mistakes:
Thinking test passes because empty string is valid
Assuming test skips null input
Confusing compilation errors with runtime failures
5. You want to test a method that should accept null, empty, and non-empty strings. Which combination of annotations correctly tests all these cases in one parameterized test?
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.