What is the purpose of the @DisplayName annotation in JUnit?
The @DisplayName annotation is used to give a test method or class a readable and descriptive name that appears in test reports and IDEs, making tests easier to understand.
Click to reveal answer
beginner
How do you use @DisplayName in a JUnit test method?
You place @DisplayName("Your readable name") above the test method to set a custom name that will show in test results instead of the method name.
Click to reveal answer
intermediate
Can @DisplayName be used on test classes as well as test methods?
Yes, @DisplayName can be applied to both test classes and test methods to provide readable names at both levels.
Click to reveal answer
beginner
What happens if you do not use @DisplayName in your tests?
If @DisplayName is not used, the test runner shows the method or class names as they are written in code, which may be less readable or descriptive.
Click to reveal answer
beginner
Give an example of a @DisplayName annotation for a test method.
Example: @DisplayName("Check if user login succeeds with valid credentials")
void testUserLogin() { ... }
Click to reveal answer
What does the @DisplayName annotation do in JUnit?
ASets a readable name for tests in reports and IDEs
BMarks a test as disabled
CSpecifies the order of test execution
DDefines a test timeout
✗ Incorrect
The @DisplayName annotation sets a readable and descriptive name for test classes or methods.
Where can you apply the @DisplayName annotation?
AOn both test classes and test methods
BOnly on test classes
COnly on test methods
DOnly on setup methods
✗ Incorrect
@DisplayName can be used on both test classes and test methods to improve readability.
If you omit @DisplayName, what name appears in test reports?
ANo name is shown
BThe method or class name as written in code
CThe package name
DA random generated name
✗ Incorrect
Without @DisplayName, the test runner shows the method or class names exactly as they appear in the code.
Which of these is a valid use of @DisplayName?
A@DisplayName(123)
B@DisplayName(true)
C@DisplayName
D@DisplayName("Test user registration")
✗ Incorrect
@DisplayName requires a string value describing the test; other types are invalid.
Why is using @DisplayName helpful?
AIt automatically fixes test failures
BIt speeds up test execution
CIt makes test reports easier to read and understand
DIt encrypts test data
✗ Incorrect
@DisplayName improves test report readability by showing descriptive names.
Explain how @DisplayName improves test readability and where you can apply it.
Think about how test names appear when running tests.
You got /3 concepts.
Describe what happens if you do not use @DisplayName in your JUnit tests.
Consider default naming behavior in test reports.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @DisplayName in JUnit tests?
easy
A. To skip a test during execution
B. To change the test execution order
C. To give tests clear, readable names for better understanding
D. To add tags for filtering 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 C
Quick Check:
@DisplayName = readable test names [OK]
Hint: Remember: @DisplayName makes test names easy to read [OK]
Common Mistakes:
Thinking @DisplayName changes test order
Confusing @DisplayName with @Tag
Assuming @DisplayName skips tests
2. Which of the following is the correct syntax to use @DisplayName in a JUnit 5 test method?
easy
A. @DisplayName = "Test addition method"
void testAdd() {}
B. @DisplayName("Test addition method")
void testAdd() {}
C. @DisplayName: "Test addition method"
void testAdd() {}
D. @DisplayName testAdd("Test addition 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 B
Quick Check:
@DisplayName("...") syntax is correct [OK]
Hint: Annotations need @ and parentheses with strings [OK]
Common Mistakes:
Using = or : instead of parentheses
Placing @DisplayName after method signature
Omitting quotes around the display name
3. Given the following test code, what will be the display name shown in the test report?
@Test
@DisplayName("Check if number is positive")
void isPositiveTest() {
assertTrue(5 > 0);
}
medium
A. isPositiveTest
B. assertTrue(5 > 0)
C. void isPositiveTest()
D. Check if number is positive
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 D
Quick Check:
@DisplayName string replaces method name [OK]
Hint: Display name shows the annotation string, not method name [OK]
Common Mistakes:
Expecting method name instead of display name
Confusing method signature with display name
Thinking assertion text appears as name
4. Identify the error in the following JUnit 5 test code using @DisplayName:
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 A
Quick Check:
@DisplayName needs quotes around text [OK]
Hint: Always put display names inside double quotes [OK]
Common Mistakes:
Omitting quotes around display name
Confusing @DisplayName with @Test
Thinking method must be public in JUnit 5
5. You want to write a JUnit 5 test method with a display name that includes quotes, like: Test "add" method works. Which is the correct way to write the @DisplayName annotation?
hard
A. @DisplayName("Test \"add\" method works")
B. @DisplayName('Test "add" method works')
C. @DisplayName("Test 'add' method works")
D. @DisplayName(Test "add" method works)
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 A
Quick Check:
Escape inner quotes with backslash in Java strings [OK]
Hint: Escape inner double quotes with \" inside @DisplayName [OK]
Common Mistakes:
Using single quotes for Java strings
Not escaping inner double quotes
Omitting quotes around the display name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.