junit

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;

@FixMethodOrder(MethodSorters.DEFAULT)
public class JUnitOrderSampleServiceTests {
	
	@Test
	public void testC() {
		System.out.println("C");
		int numberCResult = 0;
		for (int i=0;i<1000;i++) {
			//complex loop
			numberCResult++;
			
		}
		Assert.assertThat(numberCResult,isA(Integer.class));
	}
	
	@Test
	public void testA() {
		System.out.println("A");
		int numberAResult = 0;
		for (int i=0;i<10000;i++) {
			//complex loop
			numberAResult++;
			
		}
		Assert.assertThat(numberAResult,isA(Integer.class));
	}
	
	@Test
	public void testD() {
		System.out.println("D");
		int numberDResult = 0;
		for (int i=0;i<100000;i++) {
			//complex loop
			numberDResult++;
			
		}
		Assert.assertThat(numberDResult,isA(Integer.class));
	}
	
	@Test
	public void testB() {
		System.out.println("B");
		int numberBResult = 0;
		for (int i=0;i<1000000;i++) {
			//complex loop
			numberBResult++;
			
		}
		Assert.assertThat(numberBResult,isA(Integer.class));
	}
}

It starts with the annotation Download NOW!

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.

Download
You can download the full source code of this example here: Sign up
Tags

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button