Running JUnit Testing Sequentially

One problem that keep showing when im doing unit test is how can i make my unit test runs sequentially across multiple unit test classes. Usually i need this for having one unit test to start first for initializing all the data, and one unit test that run last to delete all the generated data.

Usually run multiple unit testing classes is like below image, it is something that is unpredictable and sometimes can be different between executions.

In order to make it sequential, the trick is to use JUnit with version 5.8.0 minimum.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edw</groupId>
    <artifactId>sequential-unit-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>11</java.version>
        <spring-boot-bom.version>2.3.6.Final-redhat-00001</spring-boot-bom.version>
        <junit-version>5.8.0</junit-version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>

		<!-- ....  -->

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
		
		<!-- ....  -->

    </dependencies>
</project>

And create a junit-platform.properties file,

junit.jupiter.testclass.order.default=org.junit.jupiter.api.ClassOrderer$DisplayName
junit.jupiter.testmethod.order.default=org.junit.jupiter.api.MethodOrderer$DisplayName

And the result is something like below image,

Full code for this sample can be found in below github link.

https://github.com/edwin/sequential-unit-testing

ps.
Im using unit testing’s Display Name as orderer so all my unit test classes are ordered Ascending by Display Name.

@DisplayName("03. Class Run Third")
public class SomewhatUnitTest {


    @Test
    @DisplayName("01. Test First")
    public void testSomething() throws Exception {
        assertTrue(true);
    }
}

however there are multiple other ways of doing ordering, such as by Class name or by Order annotation. Detail can be seen on below url,

https://junit.org/junit5/docs/snapshot/api/org.junit.jupiter.api/org/junit/jupiter/api/ClassOrderer.html

No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked