Close

JUnit 5 - Ordering test methods by display names, MethodOrderer.DisplayName Example

[Last Updated: Dec 22, 2025]

The MethodOrderer.DisplayName strategy orders test methods alphabetically based on their display names. This allows ordering by human-readable test descriptions rather than technical method names, making test reports more organized.

Display Name Benefits

Using @DisplayName annotations provides descriptive test names that can be ordered logically. This is particularly useful for business-readable test suites where test names describe scenarios or user stories.

Ordering Behavior

When no @DisplayName is specified, the method name is used as the display name. The ordering is case-sensitive and follows natural string comparison, similar to MethodName ordering but using the display text.

Examples

package com.logicbig.example;

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

@TestMethodOrder(MethodOrderer.DisplayName.class)
public class MethodOrdererDisplayNameTest {

    @Test
    @DisplayName("B: Process customer order")
    void processOrder() {
        System.out.println("B: Process customer order executed");
        assertTrue(true);
    }

    @Test
    @DisplayName("A: Initialize shopping cart")
    void initializeCart() {
        System.out.println("A: Initialize shopping cart executed (first due to 'A:')");
        assertTrue(true);
    }

    @Test
    @DisplayName("D: Generate invoice")
    void generateInvoice() {
        System.out.println("D: Generate invoice executed");
        assertTrue(true);
    }

    @Test
    @DisplayName("C: Calculate total price")
    void calculateTotal() {
        System.out.println("C: Calculate total price executed");
        assertTrue(true);
    }

    @Test
    void testWithoutDisplayName() {
        // Uses method name as display name
        System.out.println("testWithoutDisplayName executed (no @DisplayName - uses method name)");
        assertTrue(true);
    }
}

Output

$ mvn test -Dtest=MethodOrdererDisplayNameTest
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:junit-5-method-orderer-display-name >------
[INFO] Building junit-5-method-orderer-display-name 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-display-name ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-display-name ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-display-name ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
A: Initialize shopping cart executed (first due to 'A:')
B: Process customer order executed
C: Calculate total price executed
D: Generate invoice executed
testWithoutDisplayName executed (no @DisplayName - uses method name)
[INFO] +--com.logicbig.example.MethodOrdererDisplayNameTest - 0.106 ss
[INFO] | +-- [OK] A: Initialize shopping cart - 0.058 ss
[INFO] | +-- [OK] B: Process customer order - 0.007 ss
[INFO] | +-- [OK] C: Calculate total price - 0.002 ss
[INFO] | +-- [OK] D: Generate invoice - 0.002 ss
[INFO] | '-- [OK] testWithoutDisplayName - 0.001 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.347 s
[INFO] Finished at: 2025-12-18T12:11:03+08:00
[INFO] ------------------------------------------------------------------------
package com.logicbig.example;

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

@TestMethodOrder(MethodOrderer.DisplayName.class)
class UserRegistrationFlowTest {

    @Test
    @DisplayName("01 - Validate user input")
    void validateInput() {
        System.out.println("01 - Validate user input executed");
        assertTrue(true);
    }

    @Test
    @DisplayName("03 - Send confirmation email")
    void sendEmail() {
        System.out.println("03 - Send confirmation email executed");
        assertTrue(true);
    }

    @Test
    @DisplayName("02 - Create user account")
    void createAccount() {
        System.out.println("02 - Create user account executed");
        assertTrue(true);
    }

    @Test
    @DisplayName("04 - Log registration event")
    void logEvent() {
        System.out.println("04 - Log registration event executed");
        assertTrue(true);
    }
}

Output

$ mvn test -Dtest=UserRegistrationFlowTest
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:junit-5-method-orderer-display-name >------
[INFO] Building junit-5-method-orderer-display-name 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-display-name ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-display-name ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 3 source files with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-display-name ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
01 - Validate user input executed
02 - Create user account executed
03 - Send confirmation email executed
04 - Log registration event executed
[INFO] +--com.logicbig.example.UserRegistrationFlowTest - 0.122 ss
[INFO] | +-- [OK] 01 - Validate user input - 0.050 ss
[INFO] | +-- [OK] 02 - Create user account - 0.009 ss
[INFO] | +-- [OK] 03 - Send confirmation email - 0.001 ss
[INFO] | '-- [OK] 04 - Log registration event - 0.001 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.362 s
[INFO] Finished at: 2025-12-18T13:09:06+08:00
[INFO] ------------------------------------------------------------------------

This example shows that the ordering is case-sensitive:

package com.logicbig.example;

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

@TestMethodOrder(MethodOrderer.DisplayName.class)
class CaseSensitiveDisplayNameTest {

    @Test
    @DisplayName("apple")
    void lowerCaseTest() {
        System.out.println("'apple' test executed (lowercase)");
        assertTrue(true);
    }

    @Test
    @DisplayName("Banana")
    void mixedCaseTest() {
        // 'B' (66) comes before 'a' (97) in ASCII
        System.out.println("'Banana' test executed (uppercase B - executes first)");
        assertTrue(true);
    }

    @Test
    @DisplayName("cherry")
    void anotherLowerCaseTest() {
        System.out.println("'cherry' test executed");
        assertTrue(true);
    }
}

Output

$ mvn  test -Dtest=CaseSensitiveDisplayNameTest
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:junit-5-method-orderer-display-name >------
[INFO] Building junit-5-method-orderer-display-name 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-method-orderer-display-name ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-method-orderer-display-name ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-ordering\junit-5-method-orderer-display-name\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-method-orderer-display-name ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-method-orderer-display-name ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
'Banana' test executed (uppercase B - executes first)
'apple' test executed (lowercase)
'cherry' test executed
[INFO] +--com.logicbig.example.CaseSensitiveDisplayNameTest - 0.122 ss
[INFO] | +-- [OK] Banana - 0.060 ss
[INFO] | +-- [OK] apple - 0.010 ss
[INFO] | '-- [OK] cherry - 0.001 ss
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.515 s
[INFO] Finished at: 2025-12-18T13:09:10+08:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

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

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

    • 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

JUnit 5 - MethodOrderer.DisplayName Select All Download
  • junit-5-method-orderer-display-name
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • MethodOrdererDisplayNameTest.java

    See Also