Custom display names make test results easier to read by showing meaningful names for test parameters instead of default values.
0
0
Custom display names for parameters in JUnit
Introduction
When you want test reports to clearly show what each test case is checking.
When parameter values are complex or unclear and need explanation.
When sharing test results with non-technical team members who need easy-to-understand reports.
When debugging tests and you want to quickly identify which input caused a failure.
Syntax
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.
Examples
This test shows each animal name in the test report.
JUnit
@ParameterizedTest(name = "Animal test: {0}") @ValueSource(strings = {"cat", "dog"}) void testAnimals(String animal) { assertNotNull(animal); }
Using the
name attribute in @ParameterizedTest to customize display names with index and parameter.JUnit
@ParameterizedTest(name = "Run {index}: fruit={0}") @ValueSource(strings = {"apple", "orange"}) void testFruits(String fruit) { assertTrue(fruit.length() > 0); }
Sample Program
This test runs three times with different fruit names. The test report will show names like "Test fruit: apple" for clarity.
JUnit
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); } }
OutputSuccess
Important Notes
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.
Summary
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.