所有集成测试完成后是否可以停止重用测试容器

Cat*_*ina 6 integration-testing spring-boot testcontainers

我正在使用单例测试容器来运行多个集成测试,如下所示

    @SpringBootTest(webEnvironment = RANDOM_PORT)
    public abstract class BaseIT {
     
      static final PostgreSQLContainer<?> postgreSQLContainer;
     
      static {
        postgreSQLContainer = 
         new PostgreSQLContainer<>(DockerImageName.parse("postgres:13"))
          .withDatabaseName("test")
          .withUsername("duke")
          .withPassword("s3cret")
          .withReuse(true);
     
        postgreSQLContainer.start();
      }
     
      @DynamicPropertySource
      static void datasourceConfig(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
        registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
        registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
      }
    }
Run Code Online (Sandbox Code Playgroud)

然后从测试底座延伸出来

    @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
    class SecondApplicationIT extends BaseIT{
     
      @Autowired
      private TestRestTemplate testRestTemplate;
     
      @Autowired
      private TodoRepository todoRepository;
     
      @AfterEach
      public void cleanup() {
        this.todoRepository.deleteAll();
      }
     
      @Test
      void contextLoads() {
        this.todoRepository.saveAll(List.of(new Todo("Write blog post", LocalDateTime.now().plusDays(2)),
          new Todo("Clean appartment", LocalDateTime.now().plusDays(4))));
     
        ResponseEntity<ArrayNode> result = this.testRestTemplate.getForEntity("/todos", ArrayNode.class);
        assertEquals(200, result.getStatusCodeValue());
        assertTrue(result.getBody().isArray());
        assertEquals(2, result.getBody().size());
      }
     
    }
Run Code Online (Sandbox Code Playgroud)

但现在容器即使在SecondApplicationIT完成后仍在运行,我如何在完成后停止容器all test classes正在扩展BaseIt

P.S:我尝试@AfterEach在那里停止容器,但它不起作用

Ole*_*jev 0

您可以使用 Testcontainers提供的JUnit 扩展JUnit 扩展。

用 标记类@Testcontainers,用 标记容器字段@Container

实例字段中的容器将为每个测试用例初始化和停止。静态字段将为一个类启动一次,然后停止。

所以像这样:

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Testcontainers
public abstract class BaseIT {
 
  @Container
  static final PostgreSQLContainer<?> postgreSQLContainer = 
     new PostgreSQLContainer<>(DockerImageName.parse("postgres:13"))
      .withDatabaseName("test")
      .withUsername("duke")
      .withPassword("s3cret");
 
 
  @DynamicPropertySource
  static void datasourceConfig(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
    registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
    registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您还指定了withReuse(true),这可能会使容器不注册以进行自动生命周期管理(如果您运行测试的环境选择使用testcontainers.reuse.enabled = truein ~/.testcontainers.properties