0
0
JUnittesting~20 mins

Custom display names for parameters in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Display Name Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
}
ATest case 5
BTest with number=5
CtestNumbers with parameter 5
DtestNumbers(5)
Attempts:
2 left
💡 Hint
Look at the @ParameterizedTest annotation's name attribute and how {0} is replaced.
assertion
intermediate
2: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}")?
AassertEquals("Fruit: apple", testInfo.getDisplayName());
BassertTrue(testInfo.getDisplayName().contains("apple"));
CassertEquals("apple", testInfo.getDisplayName());
DassertNotNull(testInfo.getDisplayName());
Attempts:
2 left
💡 Hint
The display name uses the name pattern with {0} replaced by the parameter.
🔧 Debug
advanced
2: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);
}
ANullPointerException during test execution
BTest runs successfully with display names 'Value is x' and 'Value is y'
CCompilation error due to invalid annotation syntax
DIndexOutOfBoundsException at runtime
Attempts:
2 left
💡 Hint
Check the placeholder index used in the name attribute and the number of parameters.
framework
advanced
2: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);
}
AUser John is 25 years old
BUser {0} is {1} years old
CUser 25 is John years old
DtestUser(John, 25)
Attempts:
2 left
💡 Hint
The placeholders {0} and {1} correspond to the first and second parameters respectively.
🧠 Conceptual
expert
2: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?
ACustom display names can include expressions evaluated at runtime to format parameters.
BYou can use named placeholders like {name} in the name attribute to refer to parameters by name.
CThe placeholders in the name attribute must be zero-based indexes like {0}, {1}, corresponding to method parameters.
DJUnit 5 automatically generates display names using parameter types if no name attribute is provided.
Attempts:
2 left
💡 Hint
Think about how JUnit 5 replaces placeholders in the name attribute.