Testcontainersmedium3-5 years

A Spring Boot service has 20 integration test classes, each with `@Testcontainers` and its own `@Container static PostgreSQLContainer`. The suite takes 6 minutes and most of it is containers starting. How would you restructure it, how do tests stay isolated afterwards, and where does `withReuse(true)` fit?

A static @Container field is one container per class, so the suite pays the container's start 20 times. Move to one container per JVM: declare the container once as a @ServiceConnection bean in a shared @TestConfiguration that every integration test imports (or a composed @IntegrationTest annotation), so Spring's context cache keeps the bean and the container runs until the JVM exits. Isolation then comes from each test cleaning up after itself (a rolled-back transaction, or TRUNCATE in @BeforeEach), not from a fresh database. withReuse(true) goes one step further and keeps the container alive between runs on a developer machine; it is a local convenience, not a CI setting.

The lesson behind it →
More on Testcontainers