Home » Core Java » junit » JUnit Test Order Example

About Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.

JUnit Test Order Example

1. Introduction

One of the rarely used features in JUnit is the usage of @FixMethodOrder annotation. This is primarily used to indicate an order of test method calls in a specific JUnit Test case class. This is actually not a recommended practice cause we want method calls to be independent and arbitrary in nature. Test cases method should not be dependent on each other except for integration test cases.

Of course, this feature wouldn’t be included on the latest JUnit Library if not for it’s positive usage. Here are some examples that this feature might come in handy.

  • Test case to execute specific service method to satisfy a functional scenario. Bad practice but in the real world application, this can be the case.
  • Order of test case will also reflect on the representation in the report. It would certainly make sense from a report perspective that all test cases are defined in a specific order.

2. The Source

Here is a bit of example I made to showcase how can we order a specific test case.

package com.areyes1.jgc.tests;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import static org.hamcrest.CoreMatchers.*;
import org.junit.Test;
import org.junit.runners.MethodSorters;

public class JUnitOrderSampleServiceTests {
	public void testC() {
		int numberCResult = 0;
		for (int i=0;i<1000;i++) {
			//complex loop
	public void testA() {
		int numberAResult = 0;
		for (int i=0;i<10000;i++) {
			//complex loop
	public void testD() {
		int numberDResult = 0;
		for (int i=0;i<100000;i++) {
			//complex loop
	public void testB() {
		int numberBResult = 0;
		for (int i=0;i<1000000;i++) {
			//complex loop

It starts with the annotation @FixMethodOrder. This annotation will marked the test case to run on a specific order given a sorter. There are 3 types of sorters provided by the JUnit Library, these are:

  • MethodSorters.DEFAULT – By default, the test cases will run on the matter of it’s declaration in the class. We really don’t need to specify this since this will be the default sorter. According to the documentation, the default behaviour is deterministic – means that it may very well depend on a number of factors such as object creation or even placement of declaration. It is deterministic on the virtual machine level but never predictable.
  • MethodSorters.JVM – This is arbitrary one, the order is not guaranteed as it will depend on the creation of the objects in the JVM.
  • MethodSorters.NAME_ASCENDING – This will look at the method names and sort it in ascending order (A-Z).

3. Results

Here are some of the results made from each of the orders as discussed.

3.1 Using MethodSorters.DEFAULT

Figure 1.0 MethodSorters.DEFAULT result

Figure 1.0 MethodSorters.DEFAULT result

In this run, it uses the names of the methods in an ascending order. We didn’t enforce the NAME_ASCENDING sort in this method and it can vary in a few runs.

3.2 Using MethodSorters.JVM

Figure 2.0 MethodSorters.JVM Result

Figure 2.0 MethodSorters.JVM result

In this run, it uses the sequence of it’s declaration on the Test case class. Indicating that the sequence was the actual creation of the methods in the stack.

3.3 Using MethodSorters.NAME_ASCENDING

Figure 3.0 MethodSorters.NAME_ASCENDING result

Figure 3.0 MethodSorters.NAME_ASCENDING result

In this run, its the same as the MethodSorters.DEFAULT. In this however, we are enforcing this order. The default one is completely arbitrary in nature and may not be always the case.

4. Download the Eclipse project

This was an example of JUnit Test Case Order.

You can download the full source code of this example here: junit-test-order

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments