Challenge - 5 Problems
JUnit Display Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a parameterized test with custom display names
Consider this JUnit 5 parameterized test snippet using @ValueSource and @DisplayName. What will be the display name shown for the test case with parameter value 5?
JUnit
@ParameterizedTest(name = "Test with number={0}") @ValueSource(ints = {3, 5, 7}) void testNumbers(int number) { assertTrue(number > 0); }
Attempts:
2 left
💡 Hint
Look at the @ParameterizedTest annotation's name attribute and how {0} is replaced.
✗ Incorrect
The name attribute in @ParameterizedTest uses {0} as a placeholder for the first parameter value. So for parameter 5, the display name is 'Test with number=5'.
❓ assertion
intermediate2:00remaining
Assertion on display name in a parameterized test
In a JUnit 5 parameterized test, which assertion correctly verifies that the display name for the parameter value "apple" is "Fruit: apple" when using @ParameterizedTest(name = "Fruit: {0}")?
Attempts:
2 left
💡 Hint
The display name uses the name pattern with {0} replaced by the parameter.
✗ Incorrect
The display name is exactly 'Fruit: apple' because the name pattern is 'Fruit: {0}' and {0} is replaced by the parameter value 'apple'.
🔧 Debug
advanced2:00remaining
Identify the error in custom display name usage
What error will occur when running this JUnit 5 parameterized test with the following annotation?
@ParameterizedTest(name = "Value is {1}")
@ValueSource(strings = {"x", "y"})
void testValues(String val) {
assertNotNull(val);
}
JUnit
@ParameterizedTest(name = "Value is {1}") @ValueSource(strings = {"x", "y"}) void testValues(String val) { assertNotNull(val); }
Attempts:
2 left
💡 Hint
Check the placeholder index used in the name attribute and the number of parameters.
✗ Incorrect
The placeholder {1} refers to the second parameter, but the test method has only one parameter. This causes an IndexOutOfBoundsException at runtime when JUnit tries to replace {1}.
❓ framework
advanced2:00remaining
Custom display names with @CsvSource in JUnit 5
Given this parameterized test using @CsvSource with custom display names, what will be the display name for the test case with parameters ("John", 25)?
@ParameterizedTest(name = "User {0} is {1} years old")
@CsvSource({"John, 25", "Jane, 30"})
void testUser(String name, int age) {
assertTrue(age > 0);
}
JUnit
@ParameterizedTest(name = "User {0} is {1} years old") @CsvSource({"John, 25", "Jane, 30"}) void testUser(String name, int age) { assertTrue(age > 0); }
Attempts:
2 left
💡 Hint
The placeholders {0} and {1} correspond to the first and second parameters respectively.
✗ Incorrect
The display name replaces {0} with 'John' and {1} with 25, resulting in 'User John is 25 years old'.
🧠 Conceptual
expert2:00remaining
Understanding limitations of custom display names in JUnit 5 parameterized tests
Which statement about custom display names in JUnit 5 parameterized tests is TRUE?
Attempts:
2 left
💡 Hint
Think about how JUnit 5 replaces placeholders in the name attribute.
✗ Incorrect
JUnit 5 uses zero-based numeric placeholders like {0}, {1} to refer to parameters by position. Named placeholders or runtime expressions are not supported in the name attribute.