Spring boot tests, multiple test classes run in particular order (class order, not the method order)

Viewed 590

I am using Spring boot 2.3, and have the following test.

// imports


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(Suite.class)
@Suite.SuiteClasses({
        AuthControllerTests.class, //test case 1
        ReferenceDataControllerTests.class,     //test case 2
        ManualUploadDataControllerTests.class       // test case 3
})
public abstract class ApplicationTests {

    private static final PostgreSQLContainer postgreSQLContainer;

    static {
        postgreSQLContainer = new PostgreSQLContainer<>("postgres:12")
                                .withDatabaseName("tescontainers")
                                .withUsername("testcontainers")
                                .withPassword("testcontainers");

        postgreSQLContainer.start();
    }

    @DynamicPropertySource
    static void postgresqlProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
        registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
        registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
    }

}

The 3 test classes (AuthControllerTests, ReferenceDataControllerTests, ManualUploadDataControllerTests) are as below:

@AutoConfigureMockMvc
public class AuthControllerTests extends ApplicationTests {
..
..
}

@AutoConfigureMockMvc
public class ReferenceDataControllerTests  extends ApplicationTests {
..
..
}

@AutoConfigureMockMvc
public class ManualUploadDataControllerTests extends ApplicationTests {
..
..
}

Basically these tests uses the same Postgres DB initiated by ApplicationTests and they all run successfully without issues.

However, I want to run these test is particular order of classes. Hence I tried using Suite.class, but it is not helping. As you can see I provided the order AuthControllerTests, ReferenceDataControllerTests and lastly ManualUploadDataControllerTests. But it is running the ManualUploadDataControllerTests first and then AuthControllerTests and then ReferenceDataControllerTests.

Any way I can run these in needed order? I don't want to combine all the tests in one class and then use @TestMethodOrder to define the order of methods.

1 Answers

@Mihir You can manager the order of calling your test implementations.

I always do this way bellow for managing the order of tests. So you can pass other beans or businnes roles and auth strategies into all methods.

using junit4 ( ordering by method name, if it's possible to use junit5 will be better )

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-${spring.profiles.active:local}.properties")
@FixMethodOrder(MethodSorters.NAME_ASCENDING) //  org.junit.FixMethodOrder; 
public class RunIntegrationTest {

   @Autowired
    private MockMvc mockMvc;
    
    @Test // org.junit.Test;
    public void TA_testingController1() throws Exception{
        Controller1.testSomeMethod1(mockMvc);
        Controller1.testSomeMethod2(mockMvc);
    }
   
    @Test // org.junit.Test;
    public void TB_testingController2() throws Exception{
        Controller2.testSomeMethod3(mockMvc);
        Controller2.testSomeMethod4(mockMvc);
    }

}

public class Controller1 {
    public static void testSomeMethod1(MockMvc mockMvc) throws Exception {
            // ...
    }
    public static void testSomeMethod2(MockMvc mockMvc) throws Exception {
            // ...
    }
}

public class Controller2 {
    public static void testSomeMethod3(MockMvc mockMvc) throws Exception {
            // ...
    }
    public static void testSomeMethod4(MockMvc mockMvc) throws Exception {
            // ...
    }
}

Using junit5


@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-${spring.profiles.active:local}.properties")
@TestMethodOrder(OrderAnnotation.class) // import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
public class RunIntegrationTest {

   @Autowired
    private MockMvc mockMvc;
    
    @Test // org.junit.jupiter.api.Test
    @Order(10)
    public void testingController1() throws Exception{
        Controller1.testSomeMethod1(mockMvc);
        Controller1.testSomeMethod2(mockMvc);
    }
   
   @Order(20)
    @Test // org.junit.jupiter.api.Test
    public void testingController2() throws Exception{
        Controller2.testSomeMethod3(mockMvc);
        Controller2.testSomeMethod4(mockMvc);
    }

}

Related