Close

JUnit 5 - Nested Class Display Names

[Last Updated: Dec 6, 2025]

The @DisplayName annotation can be applied to nested test classes to provide human-readable names in test reports, improving readability of complex nested test structures.

Display Name Benefits

Custom display names make test reports more understandable by replacing technical class names with descriptive phrases that reflect the test's purpose or context.

Example

package com.logicbig.example;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

@DisplayName("User Management System Tests")
public class NestedClassDisplayNamesTest {

    @Test
    @DisplayName("System initialization test")
    void systemInitTest() {
        assertTrue(true);
    }

    @Nested
    @DisplayName("Authentication Module")
    class AuthenticationTests {

        @Test
        @DisplayName("Valid credentials should authenticate successfully")
        void validCredentialsTest() {
            assertTrue(true);
        }

        @Test
        @DisplayName("Invalid credentials should be rejected")
        void invalidCredentialsTest() {
            assertTrue(true);
        }

        @Nested
        @DisplayName("Password Validation")
        class PasswordValidationTests {

            @Test
            @DisplayName("Password meets minimum length requirement")
            void passwordLengthTest() {
                assertTrue(true);
            }

            @Test
            @DisplayName("Password contains required special characters")
            void passwordSpecialCharsTest() {
                assertTrue(true);
            }
        }
    }

    @Nested
    @DisplayName("Authorization Module")
    class AuthorizationTests {

        @Test
        @DisplayName("Admin users have full access")
        void adminAccessTest() {
            assertTrue(true);
        }

        @Test
        @DisplayName("Regular users have limited access")
        void regularUserAccessTest() {
            assertTrue(true);
        }

        @Nested
        @DisplayName("Role-Based Access Control")
        class RoleBasedTests {

            @Test
            @DisplayName("Manager role can approve requests")
            void managerApprovalTest() {
                assertTrue(true);
            }

            @Test
            @DisplayName("Viewer role can only read data")
            void viewerReadOnlyTest() {
                assertTrue(true);
            }
        }
    }

    @Nested
    @DisplayName("Error Handling Scenarios")
    class ErrorHandlingTests {

        @Test
        @DisplayName("System handles network timeout gracefully")
        void networkTimeoutTest() {
            assertTrue(true);
        }

        @Test
        @DisplayName("Database connection errors are logged")
        void databaseErrorTest() {
            assertTrue(true);
        }
    }
}
mvn test -Dtest=NestedClassDisplayNamesTest

Output

D:\example-projects\junit-5\nested-classes\junit-5-nested-class-display-names>mvn test -Dtest=NestedClassDisplayNamesTest
[INFO] Scanning for projects...
[INFO]
[INFO] ----------< com.logicbig.example:nested-class-display-names >-----------
[INFO] Building nested-class-display-names 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ nested-class-display-names ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\nested-classes\junit-5-nested-class-display-names\src\main\resources
[INFO]
[INFO] --- compiler:3.14.1:compile (default-compile) @ nested-class-display-names ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ nested-class-display-names ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\nested-classes\junit-5-nested-class-display-names\src\test\resources
[INFO]
[INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ nested-class-display-names ---
[INFO] Nothing to compile - all classes are up to date.
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ nested-class-display-names ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] +--User Management System Tests - 0.012 ss
[INFO] | +-- [OK] System initialization test - 0.031 ss
[INFO] | +-- [OK] Regular users have limited access - 0.009 ss
[INFO] | +-- [OK] Admin users have full access - 0.003 ss
[INFO] | +-- [OK] Manager role can approve requests - 0.002 ss
[INFO] | '-- [OK] Viewer role can only read data - 0.002 ss
[INFO] +--Error Handling Scenarios - 0.014 ss
[INFO] | +-- [OK] System handles network timeout gracefully - 0.003 ss
[INFO] | '-- [OK] Database connection errors are logged - 0.004 ss
[INFO] +--Authentication Module - 0.010 ss
[INFO] | +-- [OK] Invalid credentials should be rejected - 0.003 ss
[INFO] | +-- [OK] Valid credentials should authenticate successfully - 0.002 ss
[INFO] | +-- [OK] Password contains required special characters - 0.002 ss
[INFO] | '-- [OK] Password meets minimum length requirement - 0.004 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.842 s
[INFO] Finished at: 2025-12-06T13:30:00+08:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

  • junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
     Version Compatibility: 5.0.0 - 6.0.1Version List
    ×

    Version compatibilities of junit-jupiter-engine with this example:

    • 5.0.0
    • 5.0.1
    • 5.0.2
    • 5.0.3
    • 5.1.0
    • 5.1.1
    • 5.2.0
    • 5.3.0
    • 5.3.1
    • 5.3.2
    • 5.4.0
    • 5.4.1
    • 5.4.2
    • 5.5.0
    • 5.5.1
    • 5.5.2
    • 5.6.0
    • 5.6.1
    • 5.6.2
    • 5.6.3
    • 5.7.0
    • 5.7.1
    • 5.7.2
    • 5.8.0
    • 5.8.1
    • 5.8.2
    • 5.9.0
    • 5.9.1
    • 5.9.2
    • 5.9.3
    • 5.10.0
    • 5.10.1
    • 5.10.2
    • 5.10.3
    • 5.10.4
    • 5.10.5
    • 5.11.0
    • 5.11.1
    • 5.11.2
    • 5.11.3
    • 5.11.4
    • 5.12.0
    • 5.12.1
    • 5.12.2
    • 5.13.0
    • 5.13.1
    • 5.13.2
    • 5.13.3
    • 5.13.4
    • 5.14.0
    • 5.14.1
    • 6.0.0
    • 6.0.1

    Versions in green have been tested.

  • JDK 25
  • Maven 3.9.11

Nested Class Display Names Select All Download
  • junit-5-nested-class-display-names
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • NestedClassDisplayNamesTest.java

    See Also