What if your test reports spoke your language, not code?
Why @DisplayName for readable names in JUnit? - Purpose & Use Cases
Imagine running a big set of tests and seeing only method names like testUserLogin1 or checkPaymentProcess2 in your test report.
These names are hard to understand quickly, especially when you want to know what exactly failed or passed.
Manual test names are often technical and unclear.
Reading through them wastes time and causes confusion.
It's like reading a list of code instead of clear sentences explaining what each test does.
The @DisplayName annotation lets you write clear, human-friendly names for your tests.
This makes test reports easy to read and understand at a glance.
void testUserLogin1() { ... }
void checkPaymentProcess2() { ... }@DisplayName("User can log in successfully") void userLoginTest() { ... } @DisplayName("Payment process completes without errors") void paymentProcessTest() { ... }
It enables clear communication of test purpose, making debugging and collaboration faster and smoother.
When a test fails, instead of guessing what testUserLogin1 means, you immediately see User can log in successfully and know exactly what to fix.
Manual test names are often unclear and confusing.
@DisplayName adds readable, descriptive names to tests.
Readable names speed up understanding and fixing test results.
Practice
@DisplayName in JUnit tests?Solution
Step 1: Understand the role of @DisplayName
@DisplayName is used to provide a human-friendly name to a test method instead of the method name.Step 2: Identify the benefit of readable names
This helps both technical and non-technical people quickly understand what the test checks when reading reports.Final Answer:
To give tests clear, readable names for better understanding -> Option CQuick Check:
@DisplayName = readable test names [OK]
- Thinking @DisplayName changes test order
- Confusing @DisplayName with @Tag
- Assuming @DisplayName skips tests
@DisplayName in a JUnit 5 test method?Solution
Step 1: Recall correct annotation syntax
Annotations in Java use @AnnotationName("string") before the method signature.Step 2: Match syntax with options
@DisplayName("Test addition method") void testAdd() {} correctly uses @DisplayName with parentheses and a string argument before the method.Final Answer:
@DisplayName("Test addition method") void testAdd() {} -> Option BQuick Check:
@DisplayName("...") syntax is correct [OK]
- Using = or : instead of parentheses
- Placing @DisplayName after method signature
- Omitting quotes around the display name
@Test
@DisplayName("Check if number is positive")
void isPositiveTest() {
assertTrue(5 > 0);
}Solution
Step 1: Identify the effect of @DisplayName
The @DisplayName annotation overrides the default method name shown in reports with the given string.Step 2: Check the string provided
The string "Check if number is positive" will be shown instead of the method name.Final Answer:
Check if number is positive -> Option DQuick Check:
@DisplayName string replaces method name [OK]
- Expecting method name instead of display name
- Confusing method signature with display name
- Thinking assertion text appears as name
@DisplayName:@Test
@DisplayName(Test addition)
void testAdd() {
assertEquals(4, 2 + 2);
}Solution
Step 1: Check @DisplayName syntax
The display name must be a string literal enclosed in double quotes.Step 2: Identify missing quotes
Here, Test addition is not in quotes, causing a syntax error.Final Answer:
Missing quotes around the display name string -> Option AQuick Check:
@DisplayName needs quotes around text [OK]
- Omitting quotes around display name
- Confusing @DisplayName with @Test
- Thinking method must be public in JUnit 5
Test "add" method works. Which is the correct way to write the @DisplayName annotation?Solution
Step 1: Understand string escaping in Java annotations
Double quotes inside a Java string must be escaped with a backslash (\") to be valid.Step 2: Check each option for correct escaping
@DisplayName("Test \"add\" method works") correctly escapes the inner quotes with \". @DisplayName('Test "add" method works') uses single quotes which are invalid for Java strings. @DisplayName("Test 'add' method works") uses single quotes inside double quotes which is valid but changes the quote style. @DisplayName(Test "add" method works) lacks quotes entirely, causing syntax error.Final Answer:
@DisplayName("Test \"add\" method works") -> Option AQuick Check:
Escape inner quotes with backslash in Java strings [OK]
- Using single quotes for Java strings
- Not escaping inner double quotes
- Omitting quotes around the display name
Ready to prove it?
Pass the test and this pattern moves unknown
