0
0
JUnittesting~5 mins

@NullAndEmptySource in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
@ParameterizedTest @NullAndEmptySource void testWithNullAndEmpty(String input) { assertTrue(input == null || input.isEmpty()); }
Click to reveal answer
beginner
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
Which annotation combines the effects of @NullSource and @EmptySource?
A@NullSource
B@EmptySource
C@NullAndEmptySource
D@ValueSource
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
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
Which of these is NOT provided by @NullAndEmptySource?
Arandom integers
Bempty string
Cempty collection
Dnull
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.