Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to use a custom display name for a parameter in a JUnit 5 test.
JUnit
@ParameterizedTest
@ValueSource(strings = {"apple", "banana"})
void testWithCustomName(@[1]("fruit") String fruit) {
assertNotNull(fruit);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Name instead of @DisplayName.
Using @ParameterName which does not exist.
✗ Incorrect
The @DisplayName annotation is used to provide a custom display name for a parameter in JUnit 5.
2fill in blank
mediumComplete the code to set a custom display name for a parameter in a JUnit 5 parameterized test.
JUnit
@ParameterizedTest(name = "Test with fruit: {0}") @ValueSource(strings = {"apple", "banana"}) void testFruit(@[1]("fruitName") String fruit) { assertFalse(fruit.isEmpty()); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing @DisplayName with @Name.
Trying to use @ParameterName which is not valid.
✗ Incorrect
Use @DisplayName to assign a custom name to the parameter for better test reporting.
3fill in blank
hardFix the error in the code to correctly use a custom display name for a parameter in JUnit 5.
JUnit
@ParameterizedTest
@ValueSource(strings = {"cat", "dog"})
void testAnimals(@[1]("animal") String animal) {
assertTrue(animal.length() > 0);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @DisplayNameValue which is not a valid annotation.
Using @ParamName which does not exist.
✗ Incorrect
The correct annotation to set a custom display name for a parameter is @DisplayName.
4fill in blank
hardFill both blanks to create a parameterized test with a custom display name for the parameter and a custom test name.
JUnit
@ParameterizedTest(name = "Test with number: {0}") @ValueSource(ints = {1, 2, 3}) void testNumbers(@[1]("num") int number, @[2]("value") int value) { assertEquals(number, value); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different or invalid annotations for parameters.
Using @ParameterName which is not valid.
✗ Incorrect
Both parameters use @DisplayName to set custom display names for better test reports.
5fill in blank
hardFill all three blanks to create a parameterized test with custom display names for parameters and a custom test name pattern.
JUnit
@ParameterizedTest(name = "Run {index}: {0} - {1} - {2}") @CsvSource({"apple, red, 1", "banana, yellow, 2"}) void testFruits(@[1]("fruit") String fruit, @[2]("color") String color, @[3]("count") int count) { assertNotNull(fruit); assertNotNull(color); assertTrue(count > 0); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ParameterName or other invalid annotations.
Mixing different annotations for parameters.
✗ Incorrect
All parameters use @DisplayName to provide custom names for clearer test reports.