如何在TestContainers中关闭容器的关闭?

时间:2018-01-18 17:39:47

标签: java testcontainers

我有这个用于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在一个类执行后不要停止容器,但是当它们全部完成后?

1 个答案:

答案 0 :(得分:2)

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

   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