What does the @NullAndEmptySource annotation do in JUnit?
It provides both null and empty values as arguments to a parameterized test method, allowing you to test how your code handles these special cases.
Click to reveal answer
intermediate
How is @NullAndEmptySource different from @NullSource and @EmptySource?
@NullAndEmptySource combines the effects of @NullSource (which provides null) and @EmptySource (which provides empty values like empty strings or collections) in one annotation.
Click to reveal answer
beginner
Show a simple example of a JUnit test method using @NullAndEmptySource.
Why is it useful to test with null and empty inputs?
Because null and empty inputs are common edge cases that can cause bugs if not handled properly. Testing them helps ensure your code is robust and error-free.
Click to reveal answer
intermediate
Can @NullAndEmptySource be used with any parameter type?
No, it works with parameters that can accept null and empty values, such as String, Collections, or arrays. It won't work properly with primitive types like int or boolean.
Click to reveal answer
What values does @NullAndEmptySource provide to a parameterized test?
AOnly null values
BBoth null and empty values
COnly empty values
DRandom values
✗ Incorrect
@NullAndEmptySource provides both null and empty values to test how the method handles these cases.
Which annotation combines the effects of @NullSource and @EmptySource?
A@NullSource
B@EmptySource
C@NullAndEmptySource
D@ValueSource
✗ Incorrect
@NullAndEmptySource is a shortcut that includes both null and empty values.
Can @NullAndEmptySource be used with primitive parameter types like int?
ANo, it only works with reference types
BYes, always
CYes, but only with @ValueSource
DOnly with special configuration
✗ Incorrect
@NullAndEmptySource works with reference types that can be null or empty, not primitives.
Why should you test with null and empty inputs?
ATo check performance
BTo increase code complexity
CTo test UI layout
DTo find bugs in edge cases
✗ Incorrect
Null and empty inputs are common edge cases that can cause bugs if not handled.
Which of these is NOT provided by @NullAndEmptySource?
Arandom integers
Bempty string
Cempty collection
Dnull
✗ Incorrect
@NullAndEmptySource only provides null and empty values, not random integers.
Explain how @NullAndEmptySource helps improve test coverage in parameterized tests.
Think about why null and empty inputs are important to test.
You got /4 concepts.
Describe a scenario where using @NullAndEmptySource would be beneficial in your tests.
Consider common input problems in real applications.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @NullAndEmptySource in JUnit tests?
easy
A. To run tests only when inputs are non-null and non-empty
B. To skip tests that have null or empty inputs
C. To automatically provide null and empty values as test inputs
D. To generate random test data for parameterized 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 C
Quick Check:
@NullAndEmptySource adds null and empty inputs [OK]
Hint: Remember: @NullAndEmptySource adds null and empty inputs [OK]
Common Mistakes:
Thinking it skips tests with null or empty inputs
Confusing it with random data generators
Assuming it filters out null or empty values
2. Which of the following is the correct way to use @NullAndEmptySource in a JUnit 5 parameterized test method?
easy
A. @Test
@NullAndEmptySource
void testMethod(String input) { ... }
B. @Test
@NullAndEmptySource
void testMethod() { ... }
C. @ParameterizedTest
@NullAndEmptySource
void testMethod() { ... }
D. @ParameterizedTest
@NullAndEmptySource
void testMethod(String input) { ... }
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.
5. You want to test a method that processes user input strings, including null and empty values, plus some specific strings. Which annotation combination correctly covers null, empty, and the strings "admin" and "guest" in a single parameterized test?
hard
A. @ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"admin", "guest"})
B. @ParameterizedTest
@EmptySource
@ValueSource(strings = {"admin", "guest"})
C. @ParameterizedTest
@NullSource
@ValueSource(strings = {"admin", "guest"})
D. @ParameterizedTest
@ValueSource(strings = {null, "", "admin", "guest"})
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 A
Quick Check:
Use @NullAndEmptySource plus @ValueSource for all inputs [OK]
Hint: Combine @NullAndEmptySource with @ValueSource for null, empty, and values [OK]
Common Mistakes:
Using only @EmptySource or @NullSource missing one input
Trying to put null in @ValueSource strings (not allowed)
Not combining annotations properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.