【发布时间】:2021-09-01 14:01:52
【问题描述】:
我使用的是Spring boot 2.3,并进行了以下测试。
// 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);
}
}
3 个测试类(AuthControllerTests、ReferenceDataControllerTests、ManualUploadDataControllerTests)如下:
@AutoConfigureMockMvc
public class AuthControllerTests extends ApplicationTests {
..
..
}
@AutoConfigureMockMvc
public class ReferenceDataControllerTests extends ApplicationTests {
..
..
}
@AutoConfigureMockMvc
public class ManualUploadDataControllerTests extends ApplicationTests {
..
..
}
基本上,这些测试使用由 ApplicationTests 启动的相同 Postgres DB,并且它们都成功运行而没有问题。
但是,我想运行这些测试是特定的类顺序。因此我尝试使用 Suite.class,但它没有帮助。 如您所见,我提供了 AuthControllerTests、ReferenceDataControllerTests 和最后是 ManualUploadDataControllerTests 的顺序。但它首先运行的是 ManualUploadDataControllerTests,然后是 AuthControllerTests,然后是 ReferenceDataControllerTests。
我可以按需要的顺序运行这些吗?我不想将所有测试组合在一个类中,然后使用@TestMethodOrder 来定义方法的顺序。
【问题讨论】:
标签: java spring-boot junit