Clean β’ Professional
Integration testing ensures that all your microservices, databases, message brokers, and external APIs work together correctly. Unlike monolithic apps, microservices are distributed, making testing more complex due to network calls, independent deployments, and asynchronous messaging. Integration tests validate real interactions, not just isolated service logic, helping catch issues before production.
Integration testing in microservices ensures that all services and their dependencies work together correctly. It validates:

Why itβs harder than monolith testing:
Example:
When an Order Service calls a Payment Service:
The testing pyramid helps organize microservices tests efficiently, balancing speed, coverage, and reliability:

Integration tests sit in the middle, catching real issues without running the full system, making them ideal for CI/CD pipelines.
Integration testing in microservices focuses on real interactions between independently running services.
Key Points:
Example Architecture:
[Order Service] βREST β[Payment Service] β Kafka β[Notification Service]
REST API integration ensures services communicate correctly.
Tools: WebClient, Feign, MockMvc, WebTestClient
What to test:

Example:
@SpringBootTest
@AutoConfigureMockMvc
classOrderServiceIT {
@Autowiredprivate MockMvc mockMvc;
@Test
voidshouldCreateOrderAndProcessPayment()throws Exception {
mockMvc.perform(post("/orders")
.contentType(MediaType.APPLICATION_JSON)
.content("{\\"userId\\":1,\\"amount\\":100}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.status").value("PAID"));
}
}
Microservices often rely on messaging systems like Kafka or RabbitMQ.
Integration tests should:
Example:
@Test
voidshouldConsumeEvent() {
kafkaTemplate.send("orders-topic", orderEvent);
// Assert consumer processed event
}
Testcontainers allows running real databases and services in Docker during integration tests.
Benefits:
Example: PostgreSQL container
@Testcontainers
@SpringBootTest
classOrderRepositoryIT {
@Container
static PostgreSQLContainer<?> postgres =newPostgreSQLContainer<>("postgres:15");
@DynamicPropertySource
staticvoidproperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
}
}
Use WireMock to simulate third-party APIs:
Example:
stubFor(get("/inventory/1")
.willReturn(aResponse()
.withStatus(200)
.withBody("{\\"available\\":true}")));
Both Contract Testing and Integration Testing are important in microservices, but they solve different problems.
| Aspect | Contract Testing | Integration Testing |
|---|---|---|
| Purpose | Ensures API agreements between consumer and provider | Validates real interactions between services |
| Focus | API request & response structure | End-to-end service communication |
| Dependencies | Mocked provider or consumer | Real services & infrastructure |
| Detects | Breaking API changes | Network, config, data, and runtime issues |
| Speed | Fast | Medium |
| Tools | Pact, Spring Cloud Contract | Spring Boot Test, Testcontainers, WireMock |
| Best Use Case | Prevent API breaking changes | Verify services work together correctly |
Resilience testing ensures your microservices stay stable even when things go wrong in production.
Example:
@Test
void shouldRetryOnFailure() {
// Simulate a failed REST call
// Verify retry logic or fallback behavior
}
Integration testing in microservices ensures that services, databases, and messaging systems work together correctly.
It helps to:
When done right, integration testing builds confidence, stabilizes releases, and makes microservices reliable and scalable.