Simple definition#
An integration test checks that components work together with real infrastructure and framework behaviour: HTTP mapping and JSON, transactions and SQL, security rules, messaging. Spring Boot provides @SpringBootTest for the whole application and test slices for one layer. Testcontainers starts real databases and brokers in Docker for tests.
The problem#
Unit tests with mocks cannot catch wrong SQL, a missing index, a broken JSON mapping, a security rule that is too open, or a migration that fails on PostgreSQL. H2 “compatibility mode” behaves differently from production databases. You need tests against the real thing, fast enough to run on every build.
Choosing the right test#
| Test | Loads | Speed | Use for |
|---|---|---|---|
| Unit test | Nothing | ⚡ ms | Logic |
@WebMvcTest(OrderController.class) | MVC layer only (controllers, advice, converters, security filters) | Fast | Request mapping, validation, status codes, JSON, security rules |
@DataJpaTest | JPA, repositories, Flyway/Liquibase (transactional, rolled back) | Fast–medium | Queries, mappings, migrations |
@JsonTest | Jackson | Fast | Serialisation contracts |
@RestClientTest | RestClient / RestTemplate with a mock server | Fast | HTTP client code |
@DataRedisTest, @DataMongoTest, @JdbcTest… | That store | Fast–medium | Store-specific code |
@SpringBootTest | The whole context | Slower | End-to-end inside the service; wiring; configuration |
Slice examples#
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvcTester mvc; // AssertJ-friendly MockMvc (Spring 6.2+)
@MockitoBean OrderService orders; // replaces the deprecated @MockBean
@Test
void returns404AsProblemDetail() {
given(orders.find(42)).willThrow(new OrderNotFoundException(42));
assertThat(mvc.get().uri("/api/orders/42").with(jwt()))
.hasStatus(HttpStatus.NOT_FOUND)
.bodyJson().extractingPath("$.title").isEqualTo("Not Found");
}
}
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE) // use the Testcontainers DB, not an embedded one
@Import(TestcontainersConfig.class)
class OrderRepositoryTest {
@Autowired OrderRepository repo;
@Test void findsPaidOrdersWithLines() { ... }
}
Testcontainers with @ServiceConnection#
@TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfig {
@Bean @ServiceConnection
PostgreSQLContainer postgres() { return new PostgreSQLContainer("postgres:17-alpine"); }
@Bean @ServiceConnection
KafkaContainer kafka() { return new KafkaContainer("apache/kafka-native:4.1.0"); }
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Import(TestcontainersConfig.class)
class CheckoutFlowIT {
@Autowired RestTestClient client; // Spring 7; or WebTestClient / TestRestTemplate
@Test
void placeOrderEndToEnd() {
client.post().uri("/api/orders").body(newOrder()).exchange()
.expectStatus().isCreated();
// assert DB rows, published Kafka events (Awaitility for async), etc.
}
}
flowchart LR
T["@SpringBootTest"] --> APP["Application context"]
APP --> PG[("PostgreSQL container")]
APP --> K["Kafka container"]
APP --> WM["WireMock<br/>(external HTTP APIs)"]@ServiceConnection(Boot 3.1+) auto-configures connection properties (URL, user, password, bootstrap servers) from the container. No@DynamicPropertySourceboilerplate.- Testcontainers 2.x (used with Spring Boot 4) modernised the module and package names and dropped JUnit 4 support. Check the migration notes.
- Dev-time services: run
TestApplication.mainwith the same container config (SpringApplication.from(App::main).with(TestcontainersConfig.class).run()) to develop locally against real containers. Alternatively, Docker Compose support (spring-boot-docker-compose) startscompose.yamlservices automatically. - Use WireMock (or MockServer) for third-party HTTP APIs, and Awaitility for asynchronous assertions.
Keep the suite fast: context caching#
Spring caches application contexts between test classes with identical configuration. Every different @MockitoBean set, property or profile creates a new context, which means a slow startup.
- Standardise a few base configurations (an abstract
IntegrationTestclass). - Prefer slices; avoid
@DirtiesContext. - Reuse containers across classes (static containers or singleton beans). Testcontainers’ reuse mode can speed up local runs.
- Run integration tests in parallel where they are isolated.
Contract tests between services#
- Consumer-driven contracts (Spring Cloud Contract, Pact) verify that a provider still satisfies what its consumers expect, without full end-to-end environments.
- For events, test schemas against a registry (compatibility checks) and use Testcontainers Kafka for producer and consumer tests.
A practical strategy#
| Layer | Tests |
|---|---|
| Domain and services | Unit tests (most of them) |
| Controllers | @WebMvcTest per controller: mapping, validation, errors, security |
| Repositories and migrations | @DataJpaTest + Testcontainers (the real database engine) |
| Messaging | @SpringBootTest + Testcontainers Kafka/RabbitMQ for key flows |
| Whole service | A few @SpringBootTest(RANDOM_PORT) happy-path and failure flows |
| Between services | Contract tests |
Common mistakes#
Quick check#
@WebMvcTest vs @SpringBootTest?
@WebMvcTest loads only the web layer for one or more controllers. @SpringBootTest loads the full application context.
What does @ServiceConnection do?
It derives Spring Boot connection properties automatically from a Testcontainers container bean.
Why not test with H2? It differs from production databases in SQL, types, locking and migration behaviour, so tests can pass while production fails.
What replaced @MockBean?
@MockitoBean (and @MockitoSpyBean) from Spring Framework 6.2+.
How do you keep integration tests fast?
Use slices, reuse cached contexts by standardising configuration, share containers, and avoid @DirtiesContext.
Summary#
- Use slices for layers and
@SpringBootTestfor full flows. - Test against real infrastructure with Testcontainers +
@ServiceConnection; WireMock for external APIs. - Protect context caching; add contract tests between services.
References#
- Spring Boot reference, Testing and Testcontainers support.
- Testcontainers for Java.
- Spring Framework reference, MockMvc.
- Spring Cloud Contract and WireMock.