0
0
JUnittesting~15 mins

Custom display names for parameters in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify custom display names for parameters in a parameterized JUnit test
Preconditions (2)
Step 1: Create a parameterized test method with multiple input values
Step 2: Use @ParameterizedTest and @ValueSource annotations
Step 3: Add @DisplayName and @DisplayNameGeneration annotations to customize display names
Step 4: Run the test suite
Step 5: Observe the test report or console output for custom display names of each parameterized test case
✅ Expected Result: Each parameterized test case should show the custom display name as defined by the annotations instead of default parameter values
Automation Requirements - JUnit 5
Assertions Needed:
Verify that the test method runs successfully for all parameters
Verify that the custom display names appear in the test report or console output
Best Practices:
Use @ParameterizedTest with appropriate source annotations like @ValueSource
Use @DisplayName to set a custom name for the test method
Use @DisplayNameGeneration or @DisplayName to customize parameter display names
Keep test methods simple and focused
Use assertions to verify expected behavior
Automated Solution
JUnit
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class CustomDisplayNameTest {

    @ParameterizedTest(name = "Run {index}: input={0}")
    @ValueSource(strings = {"apple", "banana", "cherry"})
    @DisplayName("Test with custom display names for parameters")
    void testWithCustomDisplayNames(String fruit) {
        // Simple assertion to check input is not empty
        assertTrue(fruit.length() > 0);
    }
}

This test class uses JUnit 5 parameterized tests to run the testWithCustomDisplayNames method with three different string inputs.

The @ParameterizedTest annotation marks the method as parameterized. The name attribute customizes the display name for each parameterized invocation, showing the index and the input value.

The @ValueSource provides the input strings.

The @DisplayName annotation sets a custom name for the test method itself.

The class-level @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) replaces underscores with spaces in method names, improving readability.

The assertion checks that the input string is not empty, a simple valid test.

When running this test, the test report or console output will show the custom display names like "Run 1: input=apple", "Run 2: input=banana", etc.

Common Mistakes - 3 Pitfalls
{'mistake': "Not using the 'name' attribute in @ParameterizedTest to customize parameter display names", 'why_bad': 'Without it, the test report shows default parameter values which may be unclear or hard to read', 'correct_approach': "Use the 'name' attribute in @ParameterizedTest to define a clear, descriptive display name pattern"}
{'mistake': 'Using @DisplayName on the parameterized test method without customizing individual parameter names', 'why_bad': 'The method name changes but individual parameter runs still show default values, causing inconsistent display names', 'correct_approach': "Combine @DisplayName for the method and 'name' attribute in @ParameterizedTest for parameter runs"}
Forgetting to import necessary JUnit 5 annotations and classes
Bonus Challenge

Now add data-driven testing with 3 different inputs using @CsvSource and customize display names for each input set

Show Hint