gpt4 book ai didi

java - 如何关闭 TestContainers 中容器的关闭?

转载 作者:行者123 更新时间:2023-12-04 16:07:00 26 4
gpt4 key购买 nike

我有这个用于 IT 测试的抽象类:

@RunWith(SpringRunner.class)
@Import(DbUnitConfig.class)
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@DbUnitConfiguration(dataSetLoader = DbUnitDataSetLoader.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
public abstract class AbstractIT {

@ClassRule
public static final DockerComposeContainer postgres =
new DockerComposeContainer(new File("src/test/resources/docker-compose-postgres.yml"))
.withExposedService("cars-user-postgres-it", 5432);

}

当我只启动一个 IT 测试类实例时,它工作正常。

但是当我启动多个测试类时,第一个会完成,其他的会因为关闭 postgres 而失败

这是来自容器的日志:

Stopping 1ykxuc_postgres-it_1 ... 

Stopping 1ykxucpostgres-it_1 ... done
Removing 1ykxuc_postgres-it_1 ...

Removing 1ykxuc_cars-user-postgres-it_1 ... done
Removing network 1ykxuc_default

如何告诉 TestContainers 不要在一个类执行后停止容器,而是在所有类执行完时停止?

最佳答案

我发现这个解决方案是解决方法。也许有更好的灵魂?

   private static final DockerComposeContainer postgres = new DockerComposeContainer(new File("src/test/resources/docker-compose-postgres.yml"))
.withExposedService("postgres-it", 5432);

/**
* static block used to workaround shutting down of container after each test class executed
* TODO: try to remove this static block and use @ClassRule
*/
static {
postgres.starting(Description.EMPTY);
}

yml文件:

version: "2"
services:
cars-user-postgres-it:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: user

关于java - 如何关闭 TestContainers 中容器的关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com