0
0
JUnittesting~10 mins

Custom display names for parameters 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 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'
ADisplayNameValue
BDisplayName
CName
DParameterName
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Name instead of @DisplayName.
Using @ParameterName which does not exist.
2fill in blank
medium

Complete 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'
AName
BDisplayNameValue
CParameterName
DDisplayName
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing @DisplayName with @Name.
Trying to use @ParameterName which is not valid.
3fill in blank
hard

Fix 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'
ADisplayName
BParamName
CName
DDisplayNameValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using @DisplayNameValue which is not a valid annotation.
Using @ParamName which does not exist.
4fill in blank
hard

Fill 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'
ADisplayName
BParameterName
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using different or invalid annotations for parameters.
Using @ParameterName which is not valid.
5fill in blank
hard

Fill 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'
ADisplayName
DParameterName
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ParameterName or other invalid annotations.
Mixing different annotations for parameters.