Testcontainers with @SpringBootTest — The Integration Pattern
Fast recall
Quick revision
Core answer
@DynamicPropertySource plumbing. Make containers static final with withReuse(true) and a shared @TestConfiguration so the same container is reused across the entire test suite, preserving Spring's context cache. The combination gives you production-fidelity tests at a few seconds of startup overhead per CI run, not per test.Whether the candidate can set up integration tests that run against the real database/broker without breaking Spring's context cache or tanking CI runtime
Creating a new container per test class with `@Container` instance fields, forcing Docker startup on every test class and breaking context caching
Mention `@ServiceConnection` (Spring Boot 3.1+), container reuse with `~/.testcontainers.properties`, and `Testcontainers.exposeHostPorts` for callbacks back to the host
Interview-ready guide
Interview answer
Testcontainers runs the real database, message broker, or cache in a Docker container during integration tests so your code talks to the same engine in tests as in production. The H2 default in @DataJpaTest is fine for trivial CRUD but silently passes tests for PostgreSQL-specific features that fail in production.
The modern Spring Boot 3.1+ pattern
- Annotate a
staticcontainer bean with@ServiceConnectionin a@TestConfigurationclass - Spring Boot auto-configures
spring.datasource.url, Kafka bootstrap servers, Redis host/port from the running container - Import the configuration into every
@SpringBootTest— the same context cache and the same container are reused across the suite
The pre-3.1 pattern
@Container static PostgreSQLContainer postgres = new PostgreSQLContainer(...)plus@DynamicPropertySourceto wire the URL into Spring properties- Verbose but works for older Spring Boot versions
The two patterns that make it fast
static finalcontainer fields — one container per JVM, not per test classwithReuse(true)plus~/.testcontainers.propertiesenabling reuse — container persists acrossmvn testruns locally
Common pitfalls
- Non-static
@Containerfields start a new container per test class — kills the context cache and slows the suite - Forgetting
@AutoConfigureTestDatabase(replace = NONE)when using@DataJpaTestwith Testcontainers — Spring replaces the DataSource with H2 - Pinning to
:latestimage tags — reproducibility issues across machines
In an interview, the strong answer is: `@ServiceConnection` + `static final` containers + `withReuse(true)` + a shared `@TestConfiguration` is the production-fidelity integration test pattern that doesn't break Spring's context cache or balloon CI time.
Testcontainers with @SpringBootTest — The Integration Pattern
Direct Answer
Use Testcontainers to run the real database, message broker, or cache in a Docker container during integration tests, eliminating H2-vs-PostgreSQL dialect drift. The modern pattern in Spring Boot 3.1+ uses @ServiceConnection which auto-wires container connection details into Spring properties — no manual @DynamicPropertySource plumbing. Make containers static final with withReuse(true) and a shared @TestConfiguration so the same container is reused across the entire test suite, preserving Spring's context cache. The combination gives you production-fidelity tests at a few seconds of startup overhead per CI run, not per test.
Common Mistakes & Pitfalls
Creating a new container per test class with @Container instance fields, forcing Docker startup on every test class and breaking context caching
Check your understanding
Answer these without looking back at the article.
- What is the difference between `@ServiceConnection` and `@DynamicPropertySource`?
- How do you use Testcontainers with Kafka — what does `KafkaContainer` provide that `EmbeddedKafka` does not?
- Why is `static` important for `@Container` fields, and what happens if you forget?
- How would you use Testcontainers in CI vs local development, and why is reuse handled differently?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.