Home Junit 5 Junit 5 test execution order with examples

Junit 5 test execution order with examples

In this article we will see, how Junit 5 test execution order works, what are all the ways to control Junit 5 method execution order and how to implement your own execution order with several examples.

1. Why do we need execute test cases in order?

Although true unit tests typically should not rely on the order in which they are executed, there are times when it is necessary to enforce a specific test method execution order – for example, test case 1 creates some data or sets some environment to continue execution of test cases 2 without fail, test case 2 depends on test case 1 execution results. Some times test case execution order is important, when writing integration tests or functional tests where the sequence of the tests is important.

Technologies used in following examples :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8

Project Structure :

Junit 5 test execution order example

2. Junit 5 default test method execution order

In Junit 5 the default test method execution order is not defined, there is no guaranty in particular execution order by default in Junit 5. But subsequent executions runs in same order.

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class Junit5_Default_Order_Test {
	
	@Test
	void test_Add() {
		System.out.println("test_Add()");
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		System.out.println("test_Multiply()");
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		System.out.println("test_Devide()");
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void test_IsPrime() {
		System.out.println("test_IsPrime()");
		assertTrue(MathUtil.isPrime(13));
	}
}

Output :

test_IsPrime()
test_Devide()
test_Add()
test_Multiply()

3. Control test methods execution order in Junit 5

In order to define execution order of test methods, the following are the different ways Junit 5 supports to control the test method execution order.

  1. Alphanumeric
  2. OrderAnnotation
  3. Random
  4. Create your own Custom Order by implementing MethodOrderer.

3.1. Execute tests in Alphanumeric order

@TestMethodOrder(Alphanumeric.class) – It executes test methods in alphanumerically.

@TestMethodOrder(Alphanumeric.class)
public class Junit5_Alphanumeric_Order_Test {
	
	@Test
	void test_Add() {
		System.out.println("test_Add()");
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		System.out.println("test_Multiply()");
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		System.out.println("test_Devide()");
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void test_IsPrime() {
		System.out.println("test_IsPrime()");
		assertTrue(MathUtil.isPrime(13));
	}
}

Output :

test_Add()
test_Devide()
test_IsPrime()
test_Multiply()

3.2. Execute tests using OrderAnnotation

@Order(1), @Order(2) ... – Execute test methods in order based on the @Order values.

@TestMethodOrder(OrderAnnotation.class)
public class Junit5_Order_Annotation_Test {
	
	@Test
	@Order(1)
	void test_Add() {
		System.out.println("test_Add()");
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	@Order(2)
	void test_Multiply() {
		System.out.println("test_Multiply()");
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	@Order(3)
	void test_Devide() {
		System.out.println("test_Devide()");
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	@Order(4)
	void test_IsPrime() {
		System.out.println("test_IsPrime()");
		assertTrue(MathUtil.isPrime(13));
	}
}

Output :

test_Add()
test_Multiply()
test_Devide()
test_IsPrime()

3.3. Execute tests using MethodOrderer.Random class

It sorts test methods pseudo-randomly and supports configuration of a custom seed.

  1. MethodOrderer.RandomMethodOrderer that orders methods pseudo-randomly and allows for concurrent execution by default. The method execution order may not be same in repeatable tests.
  2. junit.jupiter.execution.order.random.seed – You can set the custom seed value (allows long value) for this property in junit-platform.properties file. If you define custom seed, the method execution order will be the same in across repeatable tests. Because MethodOrder uses SAME_THREAD in order to ensure that the results are repeatable across executions of the test plan.
@TestMethodOrder(MethodOrderer.Random.class)
public class Junit5_RandomOrder_Annotation_Test {
	
	@Test
	@Order(1)
	void test_Add() {
		System.out.println("test_Add()");
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	@Order(2)
	void test_Multiply() {
		System.out.println("test_Multiply()");
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	@Order(3)
	void test_Devide() {
		System.out.println("test_Devide()");
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	@Order(4)
	void test_IsPrime() {
		System.out.println("test_IsPrime()");
		assertTrue(MathUtil.isPrime(13));
	}
}

Output :

test_Devide()
test_Multiply()
test_Add()
test_IsPrime()

3.4. Define your own custom order to execute test methods in order

Implement MethodOrderer to create a custom test order. The following implementation is to execute test methods in revers by their names.

public class DescendingMethodOrder implements MethodOrderer {

	@Override
    public void orderMethods(MethodOrdererContext context) {
        context.getMethodDescriptors().sort((MethodDescriptor m1, MethodDescriptor m2) -> 
        m2.getMethod().getName().compareTo(m1.getMethod().getName()));
    }
 }
@TestMethodOrder(DescendingMethodOrder.class)
public class Junit5_Custom_Order_Test {
	
	@Test
	void test_Add() {
		System.out.println("test_Add()");
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		System.out.println("test_Multiply()");
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		System.out.println("test_Devide()");
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void test_IsPrime() {
		System.out.println("test_IsPrime()");
		assertTrue(MathUtil.isPrime(13));
	}
}

Output :

test_Multiply()
test_IsPrime()
test_Devide()
test_Add()

Conclusion

In this article we have seen, how Junit 5 test execution order works, what are all the ways to control Junit 5 method execution order and how to implement your own execution order with several examples.

References

  1. Junit 5 document

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories