Challenge - 5 Problems
DisplayName Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Look at the @DisplayName annotation value.
✗ Incorrect
The @DisplayName annotation sets a custom, readable name for the test method in reports and IDEs. Here, it is "Check addition of two numbers".
❓ assertion
intermediate2: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 } }
Attempts:
2 left
💡 Hint
TestInfo.getDisplayName() returns the display name set by @DisplayName.
✗ Incorrect
The display name set by @DisplayName is "Verify user login", so the assertion must check equality with that exact string.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Check compatibility of test runners with JUnit 5 features.
✗ Incorrect
Some test runners or IDEs may not support JUnit 5's @DisplayName annotation, causing the default method name to show instead.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how test reports look and how users read test results.
✗ Incorrect
The @DisplayName annotation helps make test reports easier to understand by showing descriptive names instead of method names.
❓ framework
expert3: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(); } }; } } }
Attempts:
2 left
💡 Hint
TestTemplateInvocationContext allows customizing display names per invocation.
✗ Incorrect
Overriding getDisplayName(int) in TestTemplateInvocationContext lets you set dynamic display names for each test invocation in a TestTemplate.