Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @EmptySource instead of @NullSource for null input.
Forgetting to annotate the test with @ParameterizedTest.
✗ Incorrect
The @NullSource annotation provides a null value as input to the test method.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @NullSource instead of @EmptySource for empty string input.
Not using @ParameterizedTest annotation.
✗ Incorrect
The @EmptySource annotation provides an empty string as input to the test method.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource(strings = {""}) instead of @EmptySource.
Using @NullSource twice.
✗ Incorrect
To test both null and empty inputs, use @NullSource and @EmptySource together.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ValueSource instead of @EmptySource.
Missing one of the annotations.
✗ Incorrect
Use @NullSource and @EmptySource together to test null and empty string inputs.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CsvSource instead of @ValueSource for simple string inputs.
Forgetting to add @ParameterizedTest annotation (not shown here).
✗ Incorrect
Combine @NullSource, @EmptySource, and @ValueSource to test null, empty, and specific string inputs.