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?
✗ 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?
✗ Incorrect
@EmptySource provides empty values like empty strings or collections to test empty input handling.
Can @NullSource be used without @ParameterizedTest?
✗ Incorrect
@NullSource works only with @ParameterizedTest to supply null arguments.
What happens if you combine @NullSource and @EmptySource on a test method?
✗ 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?
✗ 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.