SpringbootTest + TestContainers:测试污染数据库后如何刷新数据库

SpringbootTest + TestContainers: how do I refresh the database after tests pollute the database

我正在使用这样的摘要 class:

@SpringBootTest(classes = MyAppApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public abstract class AbstractIntegrationTest {

    static {
        PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer().withPassword("password")
                .withUsername("postgres").withDatabaseName("MyApp");
        postgreSQLContainer.start();

        System.setProperty("spring.datasource.url", postgreSQLContainer.getJdbcUrl());
        System.setProperty("spring.datasource.password", postgreSQLContainer.getPassword());
        System.setProperty("spring.datasource.username", postgreSQLContainer.getUsername());

    }

然后我有很多测试利用 class 像这样:

public class moreTests extends AbstractIntegrationTest {

    TestRestTemplate restTemplate = new TestRestTemplate("my-user", "password"); 
    HttpHeaders headers = new HttpHeaders();

    @Test
    public void SimpleHealthCheck() {    
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);    
        ResponseEntity<String> response = restTemplate.exchange(
                createURLWithPort("/api/v1/healthcheck"),
                HttpMethod.GET, entity, String.class);    
        assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
    }

    @Test
    public void GetInst() {    
        HttpEntity<String> entity = new HttpEntity<String>(null, headers);    
        ResponseEntity<String> response = restTemplate.exchange(
                createURLWithPort("/api/v1/institutions"),
                HttpMethod.GET, entity, String.class);
        assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));    
    }

但是,我的一些测试会污染数据库。我想控制测试是否使用新数据库运行。执行此操作的规定方法是什么?

您可以在执行测试之前使用 @Before 批注清理所有内容。

或者您在执行之前在每个测试中清理。

每个测试应该相互独立。所以通常:

  • 明确并设定期望
  • 运行 测试

如果测试失败,您的数据库将处于失败状态,因此您可以检查发生了什么。

在阅读了更多有关 Spring 启动集成测试的信息后,似乎规定的方法是对破坏性(或脏)测试使用“@DirtiesContext”注释。

编辑:几个月后,我意识到@DirtiesContext 并不出色。它基本上重置了整个应用程序,这可能很昂贵。此外,@DirtiesContext 在某些情况下可能不会重置您的数据库,具体取决于您的应用程序的工作方式。我建议在每个测试 class 的 @BeforeAll 或 @AfterAll 部分中运行一个清理 SQL 脚本。这个清理SQL脚本需要仔细编写。