A microservices testing strategy
In microservices, checks are split into layers, and e2e is kept for a few critical flows. Most checks sit in unit, integration and component tests, with contract tests between services. A stub in a component test only reflects an idea of the neighbour and goes stale, so a consumer-driven contract (Pact) is verified by the provider in its own CI. A breaking change is caught before deploy without a shared environment.
System design · Microservices
Thirty services, teams deploy daily. How many e2e tests can that release take?
In a monolith one e2e run really checks everything. In a system of 30 services where every team deploys on its own, a full e2e run per change gets slow and, worse, unstable: the test is red because another service is being deployed right now or its test database is in a strange state. The team stops trusting red. This lesson is about splitting checks into layers so every change gets fast and honest feedback, and where contracts fit in.
How it works and why
Each layer answers its own question. Unit: is the logic inside correct. Integration: does the code work correctly with its own database or queue. Component test: does one service work through its own API with a real database or queue (often in a container via Testcontainers) while neighbouring services are stubbed. Contract test: do two services understand the request and response format the same way. E2E: does a critical business flow work end to end. The higher the layer, the slower, costlier and more fragile the test, so there are few of them; the lower, the more.
Test layers in microservices
A contract is needed because a stub in a component test only reflects the team's idea of the neighbour. If the neighbour changed a field, the stub does not know, the tests are green and production fails. The consumer-driven approach (Pact) closes this gap: the consumer (the calling service) describes which requests it makes and which response it expects, these expectations become the contract, and the provider (the service that serves the API) verifies in its own CI that it fulfils it. A breaking change is caught before the deploy and without a shared environment.
Example: a field was renamed
The Ledger Service decided to rename balance to available for clarity. Ledger's component tests are green because they are its own. The Transfer Service expects balance in the response.
// consumer (Transfer) expectation, shortened
{
"request": { "method": "GET", "path": "/accounts/42" },
"response": { "status": 200, "body": { "id": "42", "balance": 1500 } }
}
Without contracts only e2e or production would find this. With a contract, provider verification in Ledger's CI fails at once: the response has no balance. The Ledger team sees what exactly breaks and for whom, and either restores the field or agrees on a migration: it returns both fields until the consumer is updated.
Limits and alternatives
- A contract checks the shape (fields, types, statuses), not business logic: it will not prove that a transfer is calculated correctly.
- The format of events in a queue is a contract too: Pact supports message pacts, and schemas (Avro, JSON Schema) in a schema registry give a similar guarantee.
- An alternative without Pact: the provider publishes OpenAPI and the consumer validates its stub responses against the schema. Simpler, but it does not know which fields the consumer really uses.
- A shared e2e environment stays for a few money flows: sign-up, top-up, transfer, withdrawal.
Common mistakes
- Stubs written by hand from memory: they go stale and component tests lie in green. Generate the stub from the contract instead.
- Hundreds of e2e tests "just in case": a 40-minute run, flaky, nobody investigates failures.
- Backward compatibility is not planned: a new provider breaks old consumers because it removed a field instead of deprecating it first.
Check yourself
Ledger component tests are green but Transfer fails after the deploy. What is not covered and how to close it?
Answer
The format agreement between services is not covered: stubs know nothing about the change. Contract tests close it: the consumer publishes expectations and the provider verifies them in its own CI before the deploy.
Why not replace contract tests with more e2e?
Answer
E2E needs all services at once, is slow, unstable and does not say who broke it. A contract runs in one service's CI, is fast and immediately names the consumer-provider pair.