0

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 ?

skyho
  • 1,438
  • 2
  • 20
  • 47
  • Test cases are run independent of each other. For each test case, separate database is created and dumped once the test case is executed. – Richa Bhuwania Dec 29 '20 at 08:29
  • 1. We do not use different Textcontainers to raise the database. We need one database to work for all tests. 2. I don't consider my question a duplicate. I don't need the order of methods, I need the order of running test classes - this is important. – skyho Dec 29 '20 at 10:13

1 Answers1

0

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.

DarkMatter
  • 1,007
  • 7
  • 13
  • There is independence there . but for example, I don't need to fill the model several times to read it. For example, one class checks the creation of a model (then I need to read the created model). Another class reads model data. To avoid making a 2-time record. Tests are more than 300 and heavy and there will be even more of them. It is expensive to lift a container for each module. – skyho Dec 29 '20 at 10:17
  • 1
    Sometimes the theoretical principle doesn't match reality. For those tests that can share resources without interfering with each other, you can put them in the same class and use @BeforeAll (or @BeforeClass) to setup the shared resource. The tests themselves still need to be able to run in an arbitrary order, but the don't have to setup the resource each time. – DarkMatter Dec 29 '20 at 10:22
  • Then it's a few thousand lines. – skyho Dec 29 '20 at 10:24
  • In addition, I found an abstract class with generic types, which has deep inheritance. The class itself has more than 1000 lines. This all needs to be redone. Many others are inherited from this abstract class, etc. In my case, it will not be possible to do as you say. A lot of duplicate code, as well as a lot of non-trivial code for tests. – skyho Dec 29 '20 at 10:27
  • It's hard to give any specific recommendations without knowing your code. In general I would say that unit tests should test small distinct components as far as possible. Writing such tests is of course time consuming but incredibly valuable. The best recommendation I can give is to follow the unit test design principles as far possible, even if it means that some tests will be heavy. – DarkMatter Dec 29 '20 at 10:48