postgresql - SpringbootTest + TestContainers:测试污染数据库后如何刷新数据库
问题描述
我正在使用这样的抽象类:
@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());
}
然后我有很多测试可以利用这个类,如下所示:
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));
}
但是,我的一些测试会污染数据库。我想控制测试是否使用新数据库运行。执行此操作的规定方法是什么?
解决方案
在阅读了有关 Spring Boot 集成测试的更多信息后,似乎规定的方法是对破坏性(或脏)的测试使用“@DirtiesContext”注释。
编辑:几个月后,我意识到@DirtiesContext 并不好。它基本上重置了整个应用程序,这可能很昂贵。此外,@DirtiesContext 在某些情况下可能不会重置您的数据库,具体取决于您的应用程序的工作方式。我建议在每个测试类的 @BeforeAll 或 @AfterAll 部分运行一个清理 SQL 脚本。这个清理 SQL 脚本需要仔细编写。
推荐阅读
- jenkins - 从 jenkins 管道作业创建 github 拉取请求
- python - 需要洞察力来理解 python 中的字符串
- drupal-8 - Drupal composer install:您的需求无法解析为一组可安装的软件包
- reactjs - 无法在反应本机应用程序中使用 react-navigation-tabs 给出未找到模块的问题?
- c++ - 在 C++ 中用整数填充固定大小的 char 数组
- eclipse - 无法在 Eclipse 中为 C/C++ 插入波浪号
- python - 确保至少安装了一个额外的 Python 包
- java - Spring Cloud 流:Kafka Sink 获取备用消息
- angular - 如何使用动画更改角度 ngfor 元素顺序
- shell - 基于条件的查找和替换 csv 文件字段中的值