Order Test Execution in JUnit5 Jupiter

Nick Gibbon
Pareture
2 min readJan 13, 2019

--

By default JUnit tests are not executed in the order in which they are written. Typically, this doesn’t matter because Unit tests shouldn’t have any interdependency. There could be some set up before running tests e.g starting up a mock server maybe. But, the tests themselves shouldn’t need to worry about execution chaining.

However, JUnit is also used as a Test Runner for functional tests where it would be less unusual to care about state during execution.
A simple example:
A) Log in
B) Perform some otherwise restricted activity
( A>B ) and ( B>A ) should give entirely different results.

Indeed, one can structure functional test suites in ways to ensure the execution order doesn’t matter; but it isn’t the craziest thing in the world to want.

With the latest release of JUnit5 Jupiter (2.5.4.0-M1, December 23, 2018) one can define the order in which the Test Methods of a Class will run.

I ordered the execution of a Selenium Test Suite which I have been using to test website navigation links. The Test Suite is designed in a way where it didn’t need to be ordered. It just feels more sensible to work down the page clicking links in a top-down manner. So I tried the new feature…

Selenium Test Project Source: https://github.com/njgibbon/pareture-selenium

Test Suite Code: https://github.com/njgibbon/pareture-selenium/blob/master/src/test/java/com/njgibbon/test/selenium/pareture/suites/TestSuite1.java

As with the pom.xml in the above repo — configure for the new release:

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0-M1</version>
<scope>test</scope>
</dependency>

then, as with the code in the Test Suite:

import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
...
@TestMethodOrder(OrderAnnotation.class)
public class TestSuite1
{
...
@Test
@Order(1)
public void test1() throws InterruptedException
{
...
@Test
@Order(2)
public void test2() throws InterruptedException
{
...

And voilà!

Youtube video of the front-end tests executing in top-down order:

Release Note for specific details: https://junit.org/junit5/docs/5.4.0-M1/user-guide/index.html#writing-tests-test-execution-order

Website Source Code: https://github.com/njgibbon/pareture

Test Project: https://github.com/njgibbon/pareture-test

Test Suite Living Documentation: https://github.com/njgibbon/pareture-test/blob/master/functional-test/front-end/website-navigation/TestSuite2-WebsiteNavigation.md

Test Reporting: https://github.com/njgibbon/pareture-test/blob/master/functional-test/front-end/website-navigation/suite-reports/TestSuiteReport-12012019-TestSuite2-WebsiteNavigation.md

--

--

Nick Gibbon
Pareture

Software reliability engineer & manager in cloud infrastructure, platforms & tools.