0
0
JUnittesting~10 mins

@NullAndEmptySource in JUnit - Interactive Code Practice

Choose your learning style9 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().