【问题标题】:How to turn off shutting down of containers in TestContainers?如何关闭关闭 TestContainers 中的容器?
【发布时间】:2018-06-27 21:04:50
【问题描述】:

我有这个用于 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 而失败

这是来自 Container 的日志:

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 在一个类执行后不要停止容器,而是在所有容器都完成后停止?

【问题讨论】:

    标签: java testcontainers


    【解决方案1】:

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

       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
    

    【讨论】:

    • 当您想启动类似“单例”的容器(即每个 JVM 执行 1 个容器)时,这是推荐的解决方案:)
    • @bsideup 感谢您的意见!现在我知道我在正确的道路上
    • @bsideup 是最新版本(1.9.1)方法开始(描述描述)被标记为已弃用。目前推荐的解决方案是什么?
    • 只要使用postgres.start();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多