Bird
Raised Fist0
JUnittesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using custom display names for parameters in JUnit tests?
easy
A. To speed up test execution time
B. To make test results easier to understand by showing meaningful names
C. To automatically fix failing tests
D. To generate test data automatically

Solution

  1. Step 1: Understand the role of display names

    Custom display names help testers and developers read test reports more clearly by showing descriptive names instead of default method names.
  2. Step 2: Identify the benefit in test results

    Meaningful names improve readability and debugging, making it easier to know what each test case checks.
  3. Final Answer:

    To make test results easier to understand by showing meaningful names -> Option B
  4. Quick Check:

    Custom display names = clearer test reports [OK]
Hint: Custom names improve report clarity, not speed or fixing [OK]
Common Mistakes:
  • Thinking display names speed up tests
  • Assuming display names fix test failures
  • Confusing display names with test data generation
2. Which annotation syntax correctly sets a custom display name pattern for a parameterized test in JUnit 5?
easy
A. @ParameterizedTest(name = "Test {index} with value {0}")
B. @TestDisplay(name = "Test {index} with value {0}")
C. @DisplayNamePattern("Test {index} with value {0}")
D. @TestName("Test {index} with value {0}")

Solution

  1. Step 1: Recall the correct annotation for parameterized test names

    JUnit 5 uses @ParameterizedTest with the 'name' attribute to set custom display names for parameters.
  2. Step 2: Verify syntax correctness

    @ParameterizedTest(name = "Test {index} with value {0}") is the correct syntax; other options are invalid annotations.
  3. Final Answer:

    @ParameterizedTest(name = "Test {index} with value {0}") -> Option A
  4. Quick Check:

    @ParameterizedTest(name=...) = correct syntax [OK]
Hint: Use @ParameterizedTest(name = "...") for custom parameter names [OK]
Common Mistakes:
  • Using non-existent annotations like @TestDisplay
  • Confusing @DisplayName with parameterized name attribute
  • Misspelling annotation names
3. Given the following test code snippet, what will be the display name of the second test execution?
@ParameterizedTest(name = "Run {index}: input={0}")
@ValueSource(strings = {"apple", "banana"})
void testFruits(String fruit) { /* test code */ }
medium
A. Run 1: input=apple
B. Run 1: input=banana
C. Run 2: input=banana
D. Run 2: input=apple

Solution

  1. Step 1: Understand the index and parameter mapping

    The index starts at 1 for the first parameter, so the second test run has index 2 and parameter "banana".
  2. Step 2: Substitute values into the display name pattern

    Replacing {index} with 2 and {0} with "banana" gives "Run 2: input=banana".
  3. Final Answer:

    Run 2: input=banana -> Option C
  4. Quick Check:

    Index 2 + second value banana = Run 2: input=banana [OK]
Hint: Index starts at 1; match parameter order [OK]
Common Mistakes:
  • Assuming index starts at 0
  • Mixing parameter values between runs
  • Confusing index with parameter value
4. Identify the error in this JUnit 5 parameterized test code that tries to set a custom display name:
@ParameterizedTest(name = "Test {index} - {0}")
@ValueSource(ints = {1, 2, 3})
void testNumbers(int number) {
    assertTrue(number > 0);
}
medium
A. There is no error; the code is correct
B. The annotation @ParameterizedTest does not support 'name' attribute
C. The placeholder {0} cannot be used for int parameters
D. The method parameter type int is invalid for @ValueSource

Solution

  1. Step 1: Check if 'name' attribute is valid in @ParameterizedTest

    @ParameterizedTest supports the 'name' attribute to customize display names.
  2. Step 2: Verify placeholders and parameter types

    {0} correctly refers to the first parameter, which can be an int from @ValueSource.
  3. Step 3: Confirm method and annotation compatibility

    Using int parameter with @ValueSource(ints = {...}) is valid and assertTrue is a valid assertion.
  4. Final Answer:

    There is no error; the code is correct -> Option A
  5. Quick Check:

    Valid syntax and usage = no error [OK]
Hint: Check if syntax matches JUnit 5 docs exactly [OK]
Common Mistakes:
  • Thinking {0} can't be used with primitive types
  • Believing 'name' attribute is unsupported
  • Confusing parameter types with annotation requirements
5. You want to create a parameterized test in JUnit 5 that shows the test index, parameter value, and a custom message "Check value" in the display name. Which of the following is the correct way to write the @ParameterizedTest annotation to achieve this?
hard
A. @ParameterizedTest(name = "{index} - {0} - Check value")
B. @ParameterizedTest(name = "Test {index}: {0} - Check value")
C. @ParameterizedTest(name = "Check value - {index} - {0}")
D. All of the following are correct

Solution

  1. Step 1: Understand the 'name' attribute flexibility

    The 'name' attribute accepts any string with placeholders {index} and {0} to customize display names.
  2. Step 2: Check each option's format

    The three provided formats all correctly use {index} and {0} with additional text to form valid display names.
  3. Step 3: Confirm all options are valid

    Since all formats are syntactically correct and achieve the goal, all are acceptable.
  4. Final Answer:

    All of the following are correct -> Option D
  5. Quick Check:

    Any string with {index} and {0} works [OK]
Hint: Any string with {index} and {0} placeholders is valid [OK]
Common Mistakes:
  • Thinking only one exact format is allowed
  • Misplacing placeholders causing syntax errors
  • Forgetting to include both {index} and {0}
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard