Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource instead, which does not provide null values.
Forgetting to annotate the method with @ParameterizedTest.
✗ Incorrect
The @NullAndEmptySource annotation provides both null and empty string values to the parameterized test.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CsvSource which requires CSV formatted strings.
Using @EnumSource which is for enums, not strings.
✗ Incorrect
The @ValueSource annotation is used to provide specific string values alongside null and empty from @NullAndEmptySource.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The parameter type must be String because @NullAndEmptySource provides null and empty string values.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as parameter type which cannot be null.
Using @ValueSource annotation alone without @NullAndEmptySource.
✗ Incorrect
Use @NullAndEmptySource annotation and String parameter type to test null and empty string inputs.
5fill in blank
hardFill 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'
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().
✗ Incorrect
Use @ValueSource to provide string literals, String as parameter type, and isEmpty() method to check empty strings.