Bird
Raised Fist0
JUnittesting~20 mins

@DisplayName for readable names in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DisplayName Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of the test display name?
Given the following JUnit 5 test code, what will be the display name shown in the test report for the test method?
JUnit
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

public class SampleTest {
    @Test
    @DisplayName("Check addition of two numbers")
    void testAdd() {
        int sum = 2 + 3;
        Assertions.assertEquals(5, sum);
    }
}
ACheck addition of two numbers
BtestAdd
Cvoid testAdd()
DSampleTest.testAdd
Attempts:
2 left
💡 Hint
Look at the @DisplayName annotation value.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the display name in a JUnit 5 test?
You want to write a test that checks if the display name of a test method is "Verify user login". Which assertion code snippet correctly verifies this using JUnit 5's TestInfo parameter?
JUnit
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoginTest {
    @Test
    @DisplayName("Verify user login")
    void loginTest(TestInfo testInfo) {
        // Assertion goes here
    }
}
AassertEquals("Verify user login", testInfo.getDisplayName());
BassertEquals("loginTest", testInfo.getDisplayName());
CassertTrue(testInfo.getDisplayName().contains("login"));
DassertNotNull(testInfo.getDisplayName());
Attempts:
2 left
💡 Hint
TestInfo.getDisplayName() returns the display name set by @DisplayName.
🔧 Debug
advanced
2:00remaining
Why does the @DisplayName annotation not change the test name in the report?
Consider this JUnit 5 test class: import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class DebugTest { @Test @DisplayName("Test multiplication") public void multiplyTest() { // test code } } The test report still shows the method name "multiplyTest" instead of "Test multiplication". What is the most likely reason?
AThe test method is not public.
BThe test runner used does not support @DisplayName annotations.
CThe @DisplayName annotation is misspelled.
DJUnit 5 requires @DisplayName to be on the class, not the method.
Attempts:
2 left
💡 Hint
Check compatibility of test runners with JUnit 5 features.
🧠 Conceptual
advanced
2:00remaining
What is the main benefit of using @DisplayName in JUnit 5 tests?
Why should testers use the @DisplayName annotation in their JUnit 5 test methods?
ATo mark tests as deprecated and skip them during execution.
BTo change the method name at runtime for reflection purposes.
CTo automatically generate test data based on the display name.
DTo provide a more readable and descriptive name for tests in reports and IDEs.
Attempts:
2 left
💡 Hint
Think about how test reports look and how users read test results.
❓ framework
expert
3:00remaining
How to dynamically set @DisplayName using a custom TestTemplate in JUnit 5?
You want to create a JUnit 5 test template that runs multiple times with different inputs and sets a dynamic display name for each invocation. Which approach correctly achieves this?
JUnit
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import java.util.List;
import java.util.stream.Stream;

public class DynamicDisplayNameTest {

    @TestTemplate
    @ExtendWith(MyInvocationContextProvider.class)
    void dynamicTest(String input) {
        // test code
    }

    static class MyInvocationContextProvider implements TestTemplateInvocationContextProvider {
        @Override
        public boolean supportsTestTemplate(ExtensionContext context) {
            return true;
        }

        @Override
        public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
            return Stream.of(
                invocationContext("Input A"),
                invocationContext("Input B")
            );
        }

        private TestTemplateInvocationContext invocationContext(String name) {
            return new TestTemplateInvocationContext() {
                @Override
                public String getDisplayName(int invocationIndex) {
                    return "Test with " + name;
                }

                @Override
                public List<Object> getAdditionalArguments() {
                    return List.of(name);
                }

                @Override
                public Stream<Extension> getAdditionalExtensions() {
                    return Stream.empty();
                }
            };
        }
    }
}
ASet display name in a @BeforeEach method using TestInfo.
BUse @DisplayName("Test with " + input) directly on the test method.
COverride getDisplayName(int) in TestTemplateInvocationContext to return dynamic names.
DUse @ParameterizedTest with @ValueSource and @DisplayName annotations.
Attempts:
2 left
💡 Hint
TestTemplateInvocationContext allows customizing display names per invocation.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To give tests clear, readable names for better understanding -> Option C
  4. 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

  1. Step 1: Recall correct annotation syntax

    Annotations in Java use @AnnotationName("string") before the method signature.
  2. Step 2: Match syntax with options

    @DisplayName("Test addition method") void testAdd() {} correctly uses @DisplayName with parentheses and a string argument before the method.
  3. Final Answer:

    @DisplayName("Test addition method") void testAdd() {} -> Option B
  4. 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

  1. Step 1: Identify the effect of @DisplayName

    The @DisplayName annotation overrides the default method name shown in reports with the given string.
  2. Step 2: Check the string provided

    The string "Check if number is positive" will be shown instead of the method name.
  3. Final Answer:

    Check if number is positive -> Option D
  4. 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:
@Test
@DisplayName(Test addition)
void testAdd() {
    assertEquals(4, 2 + 2);
}
medium
A. Missing quotes around the display name string
B. Missing @Test annotation
C. assertEquals arguments are reversed
D. Method name should be public

Solution

  1. Step 1: Check @DisplayName syntax

    The display name must be a string literal enclosed in double quotes.
  2. Step 2: Identify missing quotes

    Here, Test addition is not in quotes, causing a syntax error.
  3. Final Answer:

    Missing quotes around the display name string -> Option A
  4. 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

  1. Step 1: Understand string escaping in Java annotations

    Double quotes inside a Java string must be escaped with a backslash (\") to be valid.
  2. 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.
  3. Final Answer:

    @DisplayName("Test \"add\" method works") -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard