Search code examples
javajunit5spring-boot-test

The order of execution of integration tests does not work in Srping Boot (


I use Java 11 ( I'm not looking for the order of execution of methods within a class.)

There are no posts on the site that answer my question


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Suite.SuiteClasses({TestFirst.class,
        TestSecond.class,
        AdminTest.class})
public class ApplicationTests {

}

public class TestFirst extends ApplicationTests {

@Test
void run(){
}
}

public class TestSecond extends ApplicationTests {

@Test
void run(){
}

}

public class AdminTest extends ApplicationTests {

@Test
void run(){
}

}

I use the annotation @Suite.SuiteClasses.

I assume that the test classes should be run one by one. The launch order is not followed. Each class is located in its own directory.

For Spring, it's like a separate integration test.

How can I get the classes to execute in the order I defined ?

Maybe there is another approach for this ?


Solution

  • The run order of tests is deliberately undefined. Each test is expected to setup the resources it needs and to clean up afterwards. The order in which individual @Tests are run should not matter.

    You can use the @Before and @After annotations to do some setup and cleanup for each test. There is also @BeforeAll and @AfterAll to setup/clean up on class level (They are executed before/after all/any tests in the class).

    The principle is that a unit tests is completely stand-alone and can be run independently of any other test. You should be able to pick a single test and run it.