DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat

Changing the Order of Tests in JUnit5

Learn more about changing the order of tests in JUnit 5!

Ryan Ballenger user avatar by
Ryan Ballenger
·
Apr. 18, 19 · Tutorial
Like (2)
Save
Tweet
Share
8.54K Views

Join the DZone community and get the full member experience.

Join For Free

Multiple tests within a suite are expected to be independent in most cases. If changing the order that the tests are run subsequently causes a different outcome, such as a failed test or different failed tests, it is possible, or even likely, a sign of an underlying bug in the test class or application under test.

A test could cause an inconsistency in the database that is only detected when tests are executed in a different order. Another possibility is that a test positioned at the end could inadvertently take into account a change in the data from an earlier test and fail when it is run in isolation or at the beginning of the suite.

To detect such an issue, the order that the tests are run can be modified with JUnit's TestMethodOrder annotation. It includes a parameter to designate an alternative order. The order can also be modified by implementing and using a custom orderer class.

TestMethodOrder is a type-level annotation on a class or interface that configures the MethodOrderer. It affects the order that the test methods are run, which are those methods annotated Test, RepeatedTest, ParameterizedTest, TestFactory, or TestTemplate. The MethodOrderer is an API for choosing the specific order and includes three implementations: Alphanumeric, OrderAnnotation, and Random.

By default, with no TestMethodOrder annotation on the class, parent class, or interface, the test methods are run in a consistent order but not necessarily in the order in which they exist in the class. The Alphanumeric option sorts the test methods by their names with the String’s compareTo function. The OrderAnnotation implementation causes the test methods to be run according to the number in the Order annotation on each test. The last built-in implementation,  MethodOrder.Random runs the tests randomly, although it can be seeded for repeatability. 

If a different order is required, the MethodOrderer class can be implemented for additional customization. The class consists of a single method that provides context from which the test methods are accessed and repositioned through the list of descriptors. For example, the following tests are run in a reverse alphabetic sequence with the ReverseAlphaOrderer class that implements the MethodOrderer interface.

import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Test;

@TestMethodOrder(ReverseAlphaOrderer.class)
class CustomOrderTests{

    @Test
    void alphaTest() {
      System.out.println("alpha");
    }

    @Test
    void bravoTest() {
      System.out.println("bravo");
    }

    @Test
    void charlieTest() {
      System.out.println("charlie");
    }
}
import org.junit.jupiter.api.MethodDescriptor;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.MethodOrdererContext;
import java.util.Comparator;

class ReverseAlphaOrderer implements MethodOrderer {

private Comparator<MethodDescriptor> comparator = new Comparator<MethodDescriptor>() {

@Override
public int compare(MethodDescriptor one, MethodDescriptor two) {
String twoName = two.getMethod().getName();
String oneName = one.getMethod().getName();
return twoName.compareTo(oneName);
}
};

    @Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort(comparator);     
}
}


Testing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is a Kubernetes CI/CD Pipeline?
  • Deploying Java Serverless Functions as AWS Lambda
  • How To Check Docker Images for Vulnerabilities
  • Bye Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: