Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql SpringbootTest+;TestContainers:在测试污染数据库之后,如何刷新数据库_Postgresql_Spring Boot_Spring Boot Test_Testcontainers - Fatal编程技术网

Postgresql SpringbootTest+;TestContainers:在测试污染数据库之后,如何刷新数据库

Postgresql SpringbootTest+;TestContainers:在测试污染数据库之后,如何刷新数据库,postgresql,spring-boot,spring-boot-test,testcontainers,Postgresql,Spring Boot,Spring Boot Test,Testcontainers,我使用的抽象类如下: @SpringBootTest(classes = MyAppApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") public abstract class AbstractIntegrationTest { static { PostgreSQLContainer postgreSQLContainer = new Post

我使用的抽象类如下:

@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));    
    }
然后,我有许多利用该类的测试,如下所示:

@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));    
    }
public类moreTests扩展了AbstractIntegrationTest{
TestRestTemplate restTemplate=新的TestRestTemplate(“我的用户”、“密码”);
HttpHeaders=新的HttpHeaders();
@试验
public void SimpleHealthCheck(){
HttpEntity=新的HttpEntity(空,标题);
ResponseEntity response=restemplate.exchange(
createURLWithPort(“/api/v1/healthcheck”),
HttpMethod.GET、实体、字符串.class);
断言(response.getStatusCode(),equalTo(HttpStatus.OK));
}
@试验
public void GetInst(){
HttpEntity=新的HttpEntity(空,标题);
ResponseEntity response=restemplate.exchange(
createURLWithPort(“/api/v1/institutions”),
HttpMethod.GET、实体、字符串.class);
断言(response.getStatusCode(),equalTo(HttpStatus.OK));
}

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

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

或者在执行每个测试之前进行清理

每个测试都应该相互独立。因此通常:

  • 明确并建立期望
  • 运行测试

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

在详细阅读了有关Spring boot集成测试的内容后,似乎规定的方法是对破坏性(或脏)测试使用“@DirtiesContext”注释


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

AbstractIntegrationTest
在静态初始化块中运行Postgres,此数据库实例对于您的所有测试套件都是单一的,在将spring上下文放入缓存后,使用
DirtiesContext
不会给您一个干净的数据库。在这种情况下,您需要ome自我准备清理postgres中的错误数据。这一点很好。使用我们的spring boot应用程序,在启动过程中,有一个脚本来清理数据库,作为启动的一部分……因此,是的,这可能不适用于所有人。其他人可能希望在进行破坏性测试后,使用如下注释清理他们的数据库:@Sql(脚本={“类路径:cleanUpdatebase.sql”})