3

I have this abstract class for IT tests:

@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);

}

when i'm launching only one instance of IT test class, it works OK.

But when i'm launching multiple test classes, one the first will complete, other will fail because of shut down postgres

this is the log from 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

how to tell TestContainers not to stop containers after one class execution, but when all of them finished?

4

1 回答 1

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
于 2018-01-18T18:12:08.797 回答