Bird
Raised Fist0
JUnittesting~10 mins

@NullAndEmptySource in JUnit - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use @NullAndEmptySource in a JUnit 5 parameterized test.

JUnit
@ParameterizedTest
@[1]
void testWithNullAndEmpty(String input) {
    assertTrue(input == null || input.isEmpty());
}
Drag options to blanks, or click blank then click option'
ACsvSource
BValueSource
CMethodSource
DNullAndEmptySource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource instead, which does not provide null values.
Forgetting to annotate the method with @ParameterizedTest.
2fill in blank
medium

Complete the code to combine @NullAndEmptySource with @ValueSource to test multiple string inputs.

JUnit
@ParameterizedTest
@NullAndEmptySource
@[1](strings = {"test", "example"})
void testMultipleInputs(String input) {
    assertNotNull(input);
}
Drag options to blanks, or click blank then click option'
ACsvSource
BMethodSource
CValueSource
DEnumSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CsvSource which requires CSV formatted strings.
Using @EnumSource which is for enums, not strings.
3fill in blank
hard

Fix the error in the code to correctly use @NullAndEmptySource with a parameterized test.

JUnit
@ParameterizedTest
@NullAndEmptySource
void testInput([1] input) {
    assertTrue(input == null || input.isEmpty());
}
Drag options to blanks, or click blank then click option'
AString
Bboolean
CList<String>
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using primitive types like int or boolean which cannot be null.
Using collection types which are not supported by @NullAndEmptySource.
4fill in blank
hard

Fill both blanks to create a parameterized test that uses @NullAndEmptySource and asserts input is null or empty.

JUnit
@ParameterizedTest
@[1]
void testInputIsNullOrEmpty([2] input) {
    assertTrue(input == null || input.isEmpty());
}
Drag options to blanks, or click blank then click option'
ANullAndEmptySource
BValueSource
CString
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as parameter type which cannot be null.
Using @ValueSource annotation alone without @NullAndEmptySource.
5fill in blank
hard

Fill all three blanks to write a parameterized test using @NullAndEmptySource and @ValueSource, asserting input is not null or empty for non-null values.

JUnit
@ParameterizedTest
@NullAndEmptySource
@[1](strings = {"hello", "world"})
void testNonNullInputs([2] input) {
    if (input != null) {
        assertFalse(input.[3]());
    }
}
Drag options to blanks, or click blank then click option'
AValueSource
BString
CisEmpty
DCsvSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using CsvSource which requires CSV format.
Using int as parameter type which cannot be null.
Using wrong method name instead of isEmpty().

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

  1. Step 1: Understand the role of @NullAndEmptySource

    @NullAndEmptySource is used to add null and empty values automatically to parameterized tests.
  2. Step 2: Identify its purpose in testing

    This helps test how the code behaves with missing or empty inputs without manually adding those cases.
  3. Final Answer:

    To automatically provide null and empty values as test inputs -> Option C
  4. 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

  1. Step 1: Check annotation compatibility

    @NullAndEmptySource must be used with @ParameterizedTest to supply parameters.
  2. Step 2: Verify method signature

    The test method must accept a parameter to receive the null or empty value.
  3. Final Answer:

    @ParameterizedTest @NullAndEmptySource void testMethod(String input) { ... } -> Option D
  4. Quick Check:

    @NullAndEmptySource needs @ParameterizedTest and parameter [OK]
Hint: Use @NullAndEmptySource only with @ParameterizedTest and parameters [OK]
Common Mistakes:
  • Using @NullAndEmptySource with @Test instead of @ParameterizedTest
  • Writing test methods without parameters
  • Forgetting to annotate with @ParameterizedTest
3. Given the following test code, what will be the total number of test invocations executed?
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"hello", "world"})
void testStrings(String input) {
    // test logic here
}
medium
A. 4
B. 3
C. 2
D. 5

Solution

  1. Step 1: Count inputs from @NullAndEmptySource

    @NullAndEmptySource adds two inputs: null and empty string ("").
  2. Step 2: Count inputs from @ValueSource

    @ValueSource provides two strings: "hello" and "world".
  3. Step 3: Calculate total invocations

    Total = 2 (null and empty) + 2 (hello, world) = 4 test runs.
  4. Final Answer:

    4 -> Option A
  5. Quick Check:

    2 null/empty + 2 values = 4 [OK]
Hint: Add null and empty plus other values for total tests [OK]
Common Mistakes:
  • Counting only @ValueSource inputs
  • Counting null and empty as one input
  • Forgetting that @NullAndEmptySource adds two inputs
4. Identify the error in the following JUnit test code using @NullAndEmptySource:
@ParameterizedTest
@NullAndEmptySource
void testInput() {
    // test logic
}
medium
A. Cannot use @NullAndEmptySource without @ValueSource
B. Missing parameter in test method to receive input values
C. Incorrect annotation; should use @Test instead of @ParameterizedTest
D. Test method must be static when using @NullAndEmptySource

Solution

  1. Step 1: Check method signature

    The test method has no parameters, but @NullAndEmptySource provides inputs that must be accepted.
  2. Step 2: Understand parameterized test requirements

    Parameterized test methods must have parameters matching the sources; missing parameter causes error.
  3. Final Answer:

    Missing parameter in test method to receive input values -> Option B
  4. Quick Check:

    Parameterized test needs method parameter [OK]
Hint: Parameterized tests need method parameters to accept inputs [OK]
Common Mistakes:
  • Leaving test method parameterless with @NullAndEmptySource
  • Confusing @Test and @ParameterizedTest annotations
  • Thinking @NullAndEmptySource requires @ValueSource
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

  1. Step 1: Identify annotations for null and empty

    @NullAndEmptySource adds both null and empty string automatically.
  2. Step 2: Add specific string inputs

    @ValueSource(strings = {"admin", "guest"}) adds the required specific strings.
  3. Step 3: Verify combined usage

    Combining @NullAndEmptySource with @ValueSource covers all needed inputs in one test.
  4. Final Answer:

    @ParameterizedTest @NullAndEmptySource @ValueSource(strings = {"admin", "guest"}) -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard