Reputation: 658
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?
Upvotes: 3
Views: 12232
Reputation: 121
This is now possible by setting the experimental .withReuse(true)
when setting up container:
new GenericContainer<>(IMAGE).withExposedPorts(PORT).withReuse(true);
Remember to start the container with .start()
and adding testcontainers.reuse.enable=true
to ~/.testcontainers.properties
Reference: https://www.testcontainers.org/features/reuse/
Upvotes: 1
Reputation: 658
i found this solution as workaround. Maybe there is better soultion?
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 file:
version: "2"
services:
cars-user-postgres-it:
image: postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: user
Upvotes: 2