Replace @ClassRule with `static {}` block
bsideup opened this issue · 0 comments
bsideup commented
Hi!
@ClassRule
here will restart the container in each test class:
While Spring Boot reuses the context between tests. As soon as you add one more integration test class, your tests will start failing.
It is recommended to use a "Singleton container" pattern here and replace this code:
@ClassRule
public static GenericContainer postgres = new GenericContainer("postgres:9.6.8")
.withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password")
.withEnv("POSTGRES_USER", "postgres");
with this:
public static GenericContainer postgres = new GenericContainer("postgres:9.6.8")
.withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password")
.withEnv("POSTGRES_USER", "postgres");
static {
postgres.start();
}
Or maybe even consider using the JDBC URL support:
https://www.testcontainers.org/usage/database_containers.html#jdbc-url
https://testcontainers.gitbook.io/workshop/step-4-your-first-testcontainers-integration