What if your services could promise to work together perfectly before you even run the full app?
Why Contract testing (Pact) in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine multiple teams building different parts of a big app, like a food delivery service. Each team works on their own service, like orders, payments, or notifications. They try to connect their parts by guessing how others work, often leading to surprises and broken features when they finally put everything together.
Without contract testing, teams waste time fixing bugs caused by mismatched expectations. They rely on slow, full-system tests or manual checks. This causes delays, frustration, and unhappy users because errors only show up late in the process.
Contract testing with Pact lets teams agree on clear 'contracts' that describe how services talk to each other. Each team tests their service against these contracts early and automatically. This catches mismatches fast, so integration is smooth and reliable.
// Team A calls Team B's API without clear agreement fetch('/api/payments').then(...); // might fail if API changes
// Pact contract defines expected request and response
pact.verifyInteraction({ request: {}, response: {} });It enables fast, confident integration of many services without waiting for full system tests or manual coordination.
A ride-sharing app where driver, rider, and payment services evolve independently but always communicate correctly thanks to contract testing.
Manual integration is slow and error-prone in microservices.
Contract testing defines clear communication rules between services.
This leads to faster, safer development and happier users.
Practice
Solution
Step 1: Understand contract testing role
Contract testing ensures that two services agree on how they communicate, specifically the request and response formats.Step 2: Identify Pact's function
Pact automates contract testing by creating and verifying these agreements between microservices.Final Answer:
To verify that services agree on request and response formats -> Option CQuick Check:
Contract testing = Verify service agreements [OK]
- Confusing contract testing with UI testing
- Thinking contract testing checks database schemas
- Assuming contract testing measures performance
Solution
Step 1: Identify Pact contract format
Pact contracts are written in JSON format to describe interactions between services.Step 2: Eliminate other formats
YAML, XML, and CSV are not used by Pact for contract files.Final Answer:
JSON -> Option AQuick Check:
Pact contract format = JSON [OK]
- Assuming Pact uses YAML or XML
- Confusing data formats with contract formats
- Thinking CSV can describe complex contracts
{"request": {"method": "GET", "path": "/users/123"}, "response": {"status": 200, "body": {"id": 123, "name": "Alice"}}}Solution
Step 1: Read the response status in the Pact snippet
The response object shows "status": 200, indicating a successful request.Step 2: Confirm status meaning
Status 200 means OK, so the expected response code is 200.Final Answer:
200 -> Option BQuick Check:
Response status in Pact = 200 [OK]
- Confusing request method with response status
- Ignoring the status field in the response
- Choosing common error codes instead of actual status
Solution
Step 1: Understand contract strictness
Pact expects the provider response to match the contract exactly, including fields.Step 2: Adjust contract or provider
If the provider adds a new field, the contract must be updated to reflect this change to keep tests valid.Final Answer:
Update the contract to include the extra field -> Option DQuick Check:
Provider adds field -> update contract [OK]
- Ignoring extra fields without updating contract
- Removing fields from provider causing data loss
- Disabling tests instead of fixing contract
Solution
Step 1: Identify best practice for contract sharing
Using a shared Pact broker allows teams to publish and verify contracts centrally, enabling independent development with integration confidence.Step 2: Compare alternatives
Local-only tests lack visibility; end-to-end tests are slower and less focused; manual reviews are error-prone.Final Answer:
Each team publishes their Pact contracts to a shared broker for others to verify -> Option AQuick Check:
Shared Pact broker = smooth integration [OK]
- Skipping contract tests for only end-to-end tests
- Not sharing contracts causing integration surprises
- Relying on manual API reviews
