Bird
Raised Fist0
JUnittesting~10 mins

@NullSource and @EmptySource 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 add a null input test using @NullSource.

JUnit
@ParameterizedTest
@[1]
void testWithNullInput(String input) {
    assertNull(input);
}
Drag options to blanks, or click blank then click option'
ACsvSource
BEmptySource
CValueSource(strings = {""})
DNullSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @EmptySource instead of @NullSource for null input.
Forgetting to annotate the test with @ParameterizedTest.
2fill in blank
medium

Complete the code to add an empty input test using @EmptySource.

JUnit
@ParameterizedTest
@[1]
void testWithEmptyInput(String input) {
    assertEquals("", input);
}
Drag options to blanks, or click blank then click option'
AEmptySource
BNullSource
CValueSource(strings = {"null"})
DCsvSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @NullSource instead of @EmptySource for empty string input.
Not using @ParameterizedTest annotation.
3fill in blank
hard

Fix the error in the code to test both null and empty inputs using @NullSource and @EmptySource.

JUnit
@ParameterizedTest
@NullSource
@[1]
void testWithNullAndEmpty(String input) {
    assertTrue(input == null || input.isEmpty());
}
Drag options to blanks, or click blank then click option'
ACsvSource
BEmptySource
CNullSource
DValueSource(strings = {""})
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource(strings = {""}) instead of @EmptySource.
Using @NullSource twice.
4fill in blank
hard

Fill both blanks to create a parameterized test that checks null and empty inputs with proper annotations.

JUnit
@ParameterizedTest
@[1]
@[2]
void testNullAndEmptyInputs(String input) {
    assertTrue(input == null || input.isEmpty());
}
Drag options to blanks, or click blank then click option'
ANullSource
BEmptySource
CValueSource(strings = {""})
DCsvSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource instead of @EmptySource.
Missing one of the annotations.
5fill in blank
hard

Fill all three blanks to create a parameterized test that uses @NullSource, @EmptySource, and a @ValueSource with strings.

JUnit
@ParameterizedTest
@[1]
@[2]
@[3](strings = {"test", "example"})
void testMultipleInputs(String input) {
    assertNotNull(input);
}
Drag options to blanks, or click blank then click option'
ANullSource
BEmptySource
CValueSource
DCsvSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CsvSource instead of @ValueSource for simple string inputs.
Forgetting to add @ParameterizedTest annotation (not shown here).

Practice

(1/5)
1. What is the main purpose of the @NullSource annotation in JUnit parameterized tests?
easy
A. To provide a null value as input to the test method parameter
B. To provide an empty string as input to the test method parameter
C. To provide multiple non-null values to the test method
D. To skip the test when the input is null

Solution

  1. Step 1: Understand @NullSource role

    @NullSource is used to supply a null value as input to a parameterized test method.
  2. Step 2: Differentiate from other sources

    @EmptySource provides empty values like empty strings or collections, not null. Other options do not match @NullSource behavior.
  3. Final Answer:

    To provide a null value as input to the test method parameter -> Option A
  4. Quick Check:

    @NullSource = null input [OK]
Hint: NullSource always means null input for tests [OK]
Common Mistakes:
  • Confusing @NullSource with @EmptySource
  • Thinking @NullSource provides empty strings
  • Assuming @NullSource skips tests
2. Which of the following is the correct way to use both @NullSource and @EmptySource together in a JUnit 5 parameterized test?
easy
A. @ParameterizedTest @NullSource void testMethod(@EmptySource String input) { ... }
B. @ParameterizedTest @EmptySource @NullSource void testMethod() { ... }
C. @ParameterizedTest @NullSource @EmptySource void testMethod(String input) { ... }
D. @ParameterizedTest @NullSource @EmptySource void testMethod() { ... }

Solution

  1. Step 1: Check annotation placement

    Both @NullSource and @EmptySource must be placed above the test method along with @ParameterizedTest.
  2. Step 2: Verify method signature

    The test method must accept a parameter matching the input type (e.g., String input) to receive values from sources.
  3. Final Answer:

    @ParameterizedTest @NullSource @EmptySource void testMethod(String input) { ... } -> Option C
  4. Quick Check:

    Annotations above method with parameter = correct [OK]
Hint: Put both sources above method with matching parameter [OK]
Common Mistakes:
  • Placing @EmptySource inside method parameters
  • Missing parameter in test method
  • Wrong order or missing @ParameterizedTest
3. Given the following test method, what inputs will it receive during execution?
@ParameterizedTest
@NullSource
@EmptySource
void testInput(String input) {
    System.out.println(input == null ? "null" : "'" + input + "'");
}
medium
A. Only an empty string printed as: ''
B. null and an empty string printed as: null and ''
C. Only null printed as: null
D. null, empty string, and a string with spaces

Solution

  1. Step 1: Identify inputs from annotations

    @NullSource provides a null input, and @EmptySource provides an empty string input.
  2. Step 2: Understand output behavior

    The method prints "null" if input is null, otherwise prints the input in quotes. So outputs will be "null" and "''" for empty string.
  3. Final Answer:

    null and an empty string printed as: null and '' -> Option B
  4. Quick Check:

    NullSource + EmptySource = null + "" [OK]
Hint: NullSource gives null, EmptySource gives empty string [OK]
Common Mistakes:
  • Assuming EmptySource provides null
  • Expecting extra inputs like spaces
  • Ignoring null input from NullSource
4. Consider this test code snippet:
@ParameterizedTest
@NullSource
@EmptySource
void test(String input) {
    assertNotNull(input);
}

What will happen when this test runs?
medium
A. Test fails with AssertionError due to null input
B. Test passes for all inputs
C. Compilation error because of missing imports
D. Test is skipped because of null input

Solution

  1. Step 1: Analyze inputs from sources

    @NullSource provides a null input, and @EmptySource provides an empty string input.
  2. Step 2: Check assertion behavior

    assertNotNull(input) will fail when input is null, causing an AssertionError.
  3. Final Answer:

    Test fails with AssertionError due to null input -> Option A
  4. Quick Check:

    assertNotNull fails on null input [OK]
Hint: assertNotNull fails if input is null from NullSource [OK]
Common Mistakes:
  • Thinking test passes because empty string is valid
  • Assuming test skips null input
  • Confusing compilation errors with runtime failures
5. You want to test a method that should accept null, empty, and non-empty strings. Which combination of annotations correctly tests all these cases in one parameterized test?
hard
A. @ParameterizedTest @NullSource @ValueSource(strings = {"test"}) void testMethod(String input) { ... }
B. @ParameterizedTest @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... }
C. @ParameterizedTest @NullSource @EmptySource void testMethod() { ... }
D. @ParameterizedTest @NullSource @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... }

Solution

  1. Step 1: Identify needed inputs

    You need null (from @NullSource), empty string (from @EmptySource), and a non-empty string (from @ValueSource).
  2. Step 2: Verify method signature and annotations

    Method must accept a parameter to receive inputs. @ParameterizedTest @NullSource @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... } includes all three annotations and a parameter.
  3. Final Answer:

    @ParameterizedTest @NullSource @EmptySource @ValueSource(strings = {"test"}) void testMethod(String input) { ... } -> Option D
  4. Quick Check:

    Combine NullSource, EmptySource, and ValueSource for all cases [OK]
Hint: Combine NullSource, EmptySource, and ValueSource for full coverage [OK]
Common Mistakes:
  • Omitting NullSource or EmptySource
  • Missing method parameter
  • Using annotations without @ParameterizedTest
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