Public Access
test: share one Postgres container across test classes
The per-class container was stopped under the Spring context that the next test class reused, so a second integration test class failed with "Connection is not available".
This commit is contained in:
1 parent
3351ef75f0
commit
474a5dd570
2 files changed
+12
-8
No files matched your search
@@ -5,8 +5,6 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
import org.testcontainers.junit.jupiter.Container;
|
|
||||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for integration tests.
|
* Base class for integration tests.
|
||||||
@@ -15,10 +13,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
|||||||
@Tag("IntegrationTest")
|
@Tag("IntegrationTest")
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
@ActiveProfiles("dev")
|
@ActiveProfiles("dev")
|
||||||
@Testcontainers
|
|
||||||
public abstract class AbstractIntegrationTest {
|
public abstract class AbstractIntegrationTest {
|
||||||
|
|
||||||
@Container
|
// One container per JVM, not per class: Spring caches the context across test classes,
|
||||||
|
// and a per-class container would be stopped under the cached context.
|
||||||
@ServiceConnection
|
@ServiceConnection
|
||||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17-alpine");
|
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17-alpine");
|
||||||
|
|
||||||
|
static {
|
||||||
|
postgres.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,8 +5,6 @@ import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest;
|
|||||||
import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase;
|
import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase;
|
||||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
import org.testcontainers.junit.jupiter.Container;
|
|
||||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for persistence tests.
|
* Base class for persistence tests.
|
||||||
@@ -15,10 +13,14 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
|||||||
@Tag("PersistenceTest")
|
@Tag("PersistenceTest")
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
||||||
@Testcontainers
|
|
||||||
public abstract class AbstractPersistenceTest {
|
public abstract class AbstractPersistenceTest {
|
||||||
|
|
||||||
@Container
|
// One container per JVM, not per class: Spring caches the context across test classes,
|
||||||
|
// and a per-class container would be stopped under the cached context.
|
||||||
@ServiceConnection
|
@ServiceConnection
|
||||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17-alpine");
|
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17-alpine");
|
||||||
|
|
||||||
|
static {
|
||||||
|
postgres.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user