Video in progress
What is consumer-driven contract testing, and how is it different from integration tests?
The client service writes the request and response it needs as a contract, and the provider checks in its own CI that it matches that contract. Without bringing up the whole system, the provider catches a change that would break a client right at build time.
📖 Answer
End-to-end tests need every service up in one environment: slow, flaky, and it's hard to find which service the error is in. Unit tests with mocks are fast, but the mock goes stale on its own: the provider renamed a field and the client test is still green.
A contract test sits between those two. With Pact the process goes roughly like this:
- In the
order-service(consumer) test, it writes down what it expects fromcustomer-service:GET /customers/42→200, withidandfullNamein the body. - The test runs against a mock server and a contract file is generated.
- The contract is uploaded to the Pact Broker.
- The
customer-service(provider) CI pulls all the consumer contracts from the broker and verifies them against the real application.
If the provider removes fullName, its build fails, before the deploy.
Spring Cloud Contract is similar, but usually the provider side writes the contract and stubs for the consumer are generated from it.
Note that a contract checks only what the consumer really uses, not the whole API. That's what makes it useful: the provider can freely change the fields nobody uses.
Sign up to mark studied questions and save your progress.