Prompt
I'm building a microservices system and need contract testing between services
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
Contract testing is a great fit for microservices: it helps you verify that a service provider and its consumers agree on the request/response shape and behavior, without needing full end-to-end tests for everything.
What contract testing gives you
- Early detection of breaking API changes
- Fast feedback compared with full integration tests
- Independent deployment confidence for teams
- Clear expectations between consumers and providers
Common approaches
1) Consumer-Driven Contract Testing (recommended)
Consumers define the contract based on what they actually need; providers verify they still satisfy those expectations.
Common tools:
- Pact (very popular)
- Spring Cloud Contract
- Dredd (more API-spec based)
Typical flow with Pact:
- Consumer writes tests against a mock provider.
- The test generates a contract file (“pact”).
- The contract is published to a broker or artifact store.
- Provider runs verification tests against the contract.
- Build fails if provider no longer satisfies the contract.
2) API-spec-based testing
If you already have an OpenAPI/Swagger spec:
- Generate tests from the spec
- Validate responses against schema
- Useful, but it’s not as strong as consumer-driven contracts for consumer-specific needs
Recommended setup
For a microservices architecture, a good baseline is:
- Consumer-driven contracts for critical synchronous APIs
- Schema validation for simple/public APIs
- End-to-end tests only for a few critical flows
- Event contract testing for async messaging (Kafka, RabbitMQ, etc.)
What to test in a contract
Focus on:
- HTTP method, path, query params
- Request headers/body
- Response status codes
- Response body shape and required fields
- Content types
- Error responses for important failure cases
Avoid over-specifying:
- Exact ordering unless required
- Fields the consumer doesn’t use
- Internal implementation details
Example workflow with Pact
Consumer test
- Consumer expects
GET /users/123 - Provider returns
{ id: 123, name: "Ada" } - Pact file is created and published
Provider verification
- Provider service starts in test mode
- Pact verification runs against the provider
- If provider now returns
fullNameinstead ofname, verification fails
Best practices
- Keep contracts focused on real consumer needs
- Version and publish contracts centrally
- Run verification in CI/CD for every provider change
- Use separate contracts per consumer if their needs differ
- Include negative/error cases where important
- Treat contract failures as breaking changes unless intentionally versioned
If you have event-driven services
You should also do message contract testing:
- Define event payload contracts
- Validate producers publish expected structure
- Validate consumers can handle event variations
A practical recommendation
If you’re starting from scratch:
- Pick Pact for REST/HTTP services
- Add a broker (like Pact Broker) for sharing contracts
- Integrate consumer tests into consumer CI
- Integrate provider verification into provider CI
- Add schema tests/OpenAPI for broader API checks
If you want, I can also help you with:
- a Pact setup example,
- a contract testing strategy for your stack,
- or a sample CI pipeline for consumer/provider verification.