skip to content

@DisplayName

@DisplayName gives a test a readable name with spaces and punctuation, and DisplayNameGenerator derives them systematically. Small, but it is what makes a failing build report readable to non-authors.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is the @DisplayName annotation in JUnit 5 and why would you use it?

level: juniorimportance: must knowfreq 70%

answer

  1. Custom human-readable label for tests/classes
  2. Allows spaces, special chars, emoji
  3. Cosmetic only — no effect on discovery/execution
  4. On class and method, not repeatable
  5. Explicit @DisplayName beats generated name

basics

~20 s

@DisplayName is a JUnit 5 annotation that gives a test method or class a custom, human-readable name shown in reports and IDEs instead of the method name. It can contain spaces, punctuation, and even emojis.

solid answer

~40 s

@DisplayName is a JUnit Jupiter (JUnit 5) annotation you put on a test class or a test method to override the default display name (which is otherwise the class or method name) with any string you want. That string can include spaces, special characters, and emojis, so a method like shouldRejectNegativeAmount() can read as "should reject a negative amount" in the IDE test tree, build output, and reports. It is purely cosmetic: it changes only the reported name, not test discovery, execution, or method resolution. It is single-value and not repeatable, so a given element has at most one @DisplayName. It is most valuable for making failing tests instantly understandable and for letting you write descriptive, sentence-like test intent without cramming it into a Java identifier.

go deeper

for a junior

Knows it sets a friendly test name and that it allows spaces/special characters; can add it to a method.

for a middle

Explains it is purely cosmetic, applies to classes and methods including nested/parameterized, and is non-repeatable.

for a senior

Articulates precedence over generated names and uses it deliberately as living documentation in a consistent style across a suite.

for a principal

Sets team conventions for naming (explicit @DisplayName vs. generators), weighs report readability vs. maintenance, and standardizes it in shared test config.

## What problem it solves In Java, a *test* is just a method annotated with `@Test` (from JUnit 5, a.k.a. JUnit Jupiter). By default, tools display a test by its **method name** and the test class by its **class name**. Java identifiers cannot contain spaces or most punctuation, so method names end up like `shouldReturnEmptyListWhenNoMatches`. That is hard to read, especially in a long list of failures. **`@DisplayName`** is an annotation (a piece of metadata you attach to code) that lets you supply a *custom human-readable name* for a test method or test class. Tools (the IDE test runner, Gradle/Maven console output, HTML/XML reports) show that string instead of the identifier. ## Basic usage ```java import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @DisplayName("Shopping cart") class ShoppingCartTest { @Test @DisplayName("removes an item when quantity reaches zero") void removesItemAtZeroQuantity() { // ... } } ``` In the test report this shows as `Shopping cart > removes an item when quantity reaches zero` instead of `ShoppingCartTest > removesItemAtZeroQuantity`. ## Key facts - **Where it applies:** on a test *class* and on a test *method* (including `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, and `@Nested` classes). - **What it accepts:** any non-blank string — spaces, punctuation, Unicode, and emojis are all allowed. JUnit only requires it to be non-blank. - **What it does NOT do:** it does not affect *discovery* (which methods run), *execution order*, or how JUnit resolves the method. It is purely a label. - **Not repeatable:** an element can carry at most one `@DisplayName`. Two on the same element is a configuration error. - **Precedence:** an explicit `@DisplayName` always wins over any automatically generated name (see `DisplayNameGenerator`). ## Why it matters 1. **Readability of failures.** A CI log that says `should reject a negative amount FAILED` tells you the *intent* that broke, not just an identifier. 2. **Tests as documentation.** A class full of `@DisplayName` strings reads like a behavior spec. 3. **Expressiveness beyond identifiers.** You can phrase the full sentence, including characters Java identifiers forbid. The terms used above: an **annotation** is `@`-prefixed metadata on code; a **test method** is a method run by the test framework; a **display name** is the label a tool shows for that test.

  • Does @DisplayName affect test discovery or execution order?
    No. It only changes the reported/displayed name. Discovery and execution are unaffected.
  • Can a single test method have two @DisplayName annotations?
    No, it is not repeatable; at most one per element. Two is a configuration error.

saying these in an interview costs you the question

  • Thinking @DisplayName changes which tests run or the execution order
  • Believing it can be applied multiple times to the same element
  • Confusing it with @Tag (filtering) or @Disabled (skipping)
  • Assuming an empty/blank @DisplayName is allowed

context

open as a page

What is a DisplayNameGenerator and how do you apply one to derive readable test names automatically?

level: middleimportance: should knowfreq 48%

basics

~10 s

A DisplayNameGenerator is a JUnit 5 strategy that builds a test's display name from its class/method automatically. You attach one with @DisplayNameGeneration so you do not have to write @DisplayName on every test.

open as a page

For parameterized and repeated tests, how do per-invocation names relate to @DisplayName, and what controls them?

level: middleimportance: nice to knowfreq 34%

basics

~10 s

@DisplayName names the whole parameterized or repeated test. Each individual invocation gets its own name from the test's name pattern (the name attribute), not from @DisplayName.

open as a page

How do @Nested classes combine with @DisplayName and the IndicativeSentences generator to produce readable, hierarchical test reports?

level: seniorimportance: nice to knowfreq 30%

basics

~10 s

@Nested groups related tests into inner classes, each able to carry its own @DisplayName, so reports read as a tree. IndicativeSentences joins the enclosing names with the method name into one readable sentence.

open as a page

When standardizing test naming across a large codebase, how would you decide between explicit @DisplayName, a DisplayNameGenerator default, and method-name conventions?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

Pick one default strategy for the whole codebase (often a generator like ReplaceUnderscores set via the config property), allow explicit @DisplayName to override where wording matters, and enforce it so reports stay consistent and low-maintenance.

open as a page