Custom display names make test results easier to read by showing meaningful names for test parameters instead of default values.
Custom display names for parameters in JUnit
@ParameterizedTest(name = "Test with fruit: {0}") @ValueSource(strings = {"apple", "banana"}) void testWithCustomName(String fruit) { // test code }
Use @ParameterizedTest(name = "...") to customize test names.
In JUnit 5, {0} refers to the first parameter value in the display name.
@ParameterizedTest(name = "Animal test: {0}") @ValueSource(strings = {"cat", "dog"}) void testAnimals(String animal) { assertNotNull(animal); }
name attribute in @ParameterizedTest to customize display names with index and parameter.@ParameterizedTest(name = "Run {index}: fruit={0}") @ValueSource(strings = {"apple", "orange"}) void testFruits(String fruit) { assertTrue(fruit.length() > 0); }
This test runs three times with different fruit names. The test report will show names like "Test fruit: apple" for clarity.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; class FruitTest { @ParameterizedTest(name = "Test fruit: {0}") @ValueSource(strings = {"apple", "banana", "cherry"}) void testFruitNames(String fruit) { assertNotNull(fruit); assertTrue(fruit.length() > 0); } }
Custom display names improve test report readability and help quickly identify failing cases.
Use {index} to show the test run number in the display name.
Always keep display names short and clear for best results.
Custom display names make test results easier to understand.
Use @ParameterizedTest(name = "...") or @DisplayName to set names.
Display names can include parameter values and test indexes.
Practice
Solution
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.Step 2: Identify the benefit in test results
Meaningful names improve readability and debugging, making it easier to know what each test case checks.Final Answer:
To make test results easier to understand by showing meaningful names -> Option BQuick Check:
Custom display names = clearer test reports [OK]
- Thinking display names speed up tests
- Assuming display names fix test failures
- Confusing display names with test data generation
Solution
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.Step 2: Verify syntax correctness
@ParameterizedTest(name = "Test {index} with value {0}") is the correct syntax; other options are invalid annotations.Final Answer:
@ParameterizedTest(name = "Test {index} with value {0}") -> Option AQuick Check:
@ParameterizedTest(name=...) = correct syntax [OK]
- Using non-existent annotations like @TestDisplay
- Confusing @DisplayName with parameterized name attribute
- Misspelling annotation names
@ParameterizedTest(name = "Run {index}: input={0}")
@ValueSource(strings = {"apple", "banana"})
void testFruits(String fruit) { /* test code */ }Solution
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".Step 2: Substitute values into the display name pattern
Replacing {index} with 2 and {0} with "banana" gives "Run 2: input=banana".Final Answer:
Run 2: input=banana -> Option CQuick Check:
Index 2 + second value banana = Run 2: input=banana [OK]
- Assuming index starts at 0
- Mixing parameter values between runs
- Confusing index with parameter value
@ParameterizedTest(name = "Test {index} - {0}")
@ValueSource(ints = {1, 2, 3})
void testNumbers(int number) {
assertTrue(number > 0);
}Solution
Step 1: Check if 'name' attribute is valid in @ParameterizedTest
@ParameterizedTest supports the 'name' attribute to customize display names.Step 2: Verify placeholders and parameter types
{0} correctly refers to the first parameter, which can be an int from @ValueSource.Step 3: Confirm method and annotation compatibility
Using int parameter with @ValueSource(ints = {...}) is valid and assertTrue is a valid assertion.Final Answer:
There is no error; the code is correct -> Option AQuick Check:
Valid syntax and usage = no error [OK]
- Thinking {0} can't be used with primitive types
- Believing 'name' attribute is unsupported
- Confusing parameter types with annotation requirements
Solution
Step 1: Understand the 'name' attribute flexibility
The 'name' attribute accepts any string with placeholders {index} and {0} to customize display names.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.Step 3: Confirm all options are valid
Since all formats are syntactically correct and achieve the goal, all are acceptable.Final Answer:
All of the following are correct -> Option DQuick Check:
Any string with {index} and {0} works [OK]
- Thinking only one exact format is allowed
- Misplacing placeholders causing syntax errors
- Forgetting to include both {index} and {0}
Ready to prove it?
Pass the test and this pattern moves unknown
