0
0
JUnittesting~5 mins

@NullSource and @EmptySource in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the @NullSource annotation do in JUnit?
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:<br>
@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
Which annotation supplies an empty collection or empty string to a test method?
A@NullSource
B@ValueSource
C@CsvSource
D@EmptySource
Can @NullSource be used without @ParameterizedTest?
ANo, it must be used with @ParameterizedTest
BOnly with @RepeatedTest
COnly with @Test
DYes, always
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
Which of these is NOT a typical use of @EmptySource?
ATesting null values
BTesting empty strings
CTesting empty lists
DTesting empty arrays
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.