Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What is consumer-driven contract testing, and how do Pact and Spring Cloud Contract differ?

In consumer-driven contract testing, each consumer states the requests it makes and the parts of the responses it relies on, and the provider's build verifies that it satisfies those contracts, so a breaking API change fails before anything is deployed. With Pact, the consumer's tests generate pact files that are shared through a broker and verified by the provider. With Spring Cloud Contract, contracts are usually written on the provider side in Groovy, YAML or Kotlin and generate both provider tests and WireMock stubs for consumers.

Why this question is asked

When services are deployed independently, the hardest bugs are the ones between them: the provider renames a field, makes one optional, or changes an error status, and a consumer it never heard of breaks in production. The traditional answer is a shared integration environment where everything is deployed together, which is slow, flaky and late. Interviewers ask about contract testing because it is the main alternative, and comparing the two leading tools shows whether we understand the workflow, not just the name. In this answer, we will recap what a contract is and why consumers drive it, follow the Pact workflow with code, then the Spring Cloud Contract workflow, compare them, and see what contract tests do and do not catch.

1A Quick Recap: What a Contract Is

A contract is a written agreement about an interaction between two services: when the consumer sends this request, the provider answers with a response of this shape. The consumer is the service that makes the call, and the provider is the one that answers. A contract is precise enough to run as a test on both sides. On the consumer side, it becomes a stub the consumer's client is tested against. On the provider side, it becomes a request sent to the real provider code, whose response is checked against the contract.

Consumer-driven means that the contracts come from what consumers actually use. If the provider's response has 30 fields and a consumer reads only three, its contract mentions only those three. That is the point: the provider can then change or remove the other 27 freely, and the verification tells it exactly which changes would break a real consumer. Without contracts, a provider either never removes anything for fear of breaking someone, or removes things and finds out in production.

2The Pact Workflow

Pact is a contract testing tool with implementations for many languages. The workflow starts on the consumer side. The consumer writes a test against a Pact mock server, describing the interaction it expects. When the test passes, Pact writes the interaction into a pact file, a JSON document, and the file is published to a Pact Broker, a server that stores contracts and verification results.

Java
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "customer-service")
class CustomerClientPactTest {
@Pact(consumer = "order-service")
V4Pact customer42(PactDslWithProvider builder) {
return builder
.given("customer 42 exists") // provider state
.uponReceiving("a request for customer 42")
.path("/customers/42").method("GET")
.willRespondWith()
.status(200)
.body(new PactDslJsonBody()
.integerType("id", 42) // any integer, 42 as the example
.stringType("fullName", "Ann")) // any string
.toPact(V4Pact.class);
}
@Test
void fetchesCustomer(MockServer mockServer) {
CustomerClient client = new CustomerClient(RestClient.create(mockServer.getUrl()));
assertThat(client.find(42).fullName()).isEqualTo("Ann"); // passes; pact file written
}
}
Step throughPact: consumer-driven, through a broker
Consumer buildPact BrokerProvider build
test against Pact mock server, write pact file
publish pact for order-service version 1.8
fetch pacts where customer-service is the provider
replay requests against real provider, check responses
publish verification result
can-i-deploy order-service 1.8 to production?
yes: verified by the deployed provider version

7 messages in order. Play them, or step through them one at a time.

The broker connects the two pipelines and answers whether a version is safe to deploy.

Notice the type matchers: integerType and stringType say 'any integer' and 'any string', so the contract does not require the provider to return exactly Ann. That keeps contracts about shape rather than about test data.

On the provider side, a verification test fetches the pacts for customer-service from the broker, starts the real provider, sets up each provider state such as 'customer 42 exists' through a @State method, replays each request and checks the response. The result goes back to the broker. A command called can-i-deploy then asks the broker whether a given version of a service is compatible with the versions of its partners in an environment, which lets pipelines block an incompatible deployment. The annotation and class names above follow Pact JVM's JUnit 5 support; check your Pact JVM version for the exact names.

3The Spring Cloud Contract Workflow

Spring Cloud Contract approaches the same idea from the provider side, and it is built around the Spring and JVM ecosystem. Contracts are written in a DSL, in Groovy, YAML, Kotlin or Java, and usually live in the provider's repository. Consumers propose new contracts through pull requests to that repository, so the contracts are still consumer-driven, but they are stored and reviewed with the provider's code.

YAML
# src/test/resources/contracts/customers/shouldReturnCustomer42.yml (in customer-service)
request:
method: GET
url: /customers/42
response:
status: 200
headers:
Content-Type: application/json
body:
id: 42
fullName: Ann

At build time, the Spring Cloud Contract plugin does two things with each contract. It generates a provider test, typically using RestAssuredMockMvc against a base class that sets up the controller with mocked collaborators, and runs it, so the provider cannot build if it breaks a contract. It also generates WireMock stub mappings and packages them as a stubs artifact, published to a Maven repository. Consumers then use Stub Runner, for example with @AutoConfigureStubRunner, to download the stubs and run them in a local WireMock server for their tests. The stubs the consumer tests against are therefore generated from the same contracts the provider verifies.

4Comparing Pact and Spring Cloud Contract

Both tools solve the same problem, but their workflows suit different organisations.

Pact and Spring Cloud Contract
AspectPactSpring Cloud Contract
Where contracts come fromGenerated by consumer testsWritten in a DSL, usually in the provider repo
Contract formatPact JSON filesGroovy, YAML, Kotlin or Java DSL
How they are sharedPact BrokerProvider repo plus stubs jar in Maven
Consumer tests run againstPact mock serverGenerated WireMock stubs via Stub Runner
Provider sideVerification test replays pactsGenerated tests run in the provider build
LanguagesManyJVM-focused, Spring friendly
Deployment gatecan-i-deploy against the brokerProvider build fails; broker optional

A rough rule: Pact fits organisations with several languages and teams that want consumers to drive changes independently, with a broker coordinating who is compatible with whom. Spring Cloud Contract fits Spring-heavy organisations that like contracts reviewed in the provider's repository and stubs distributed as Maven artifacts. Spring Cloud Contract can also work with Pact files and a Pact Broker, so the choice is less exclusive than it looks.

5What Contract Tests Catch and What They Do Not

Contract tests catch breaking changes to the interface between two services: a renamed or removed field, a changed type, a different status code, a new required header, a changed path. They catch them in the provider's pipeline, before deployment, and they name the consumer that would break. That replaces most of what teams used to learn from deploying everything into a shared environment, and it lets each service be deployed on its own.

They do not test everything. A contract checks the shape of a response, not whether the provider's business logic is correct; that is the provider's own tests' job. It does not test performance, timeouts or network failures; those are tested with WireMock faults in the consumer's client tests. And it does not test the real deployed wiring, such as URLs, service discovery and security between services, which is why a small end-to-end smoke suite is still worth having. Contracts also need care: a contract that specifies exact values instead of types, or fields the consumer does not actually use, ties the provider down for no benefit.

6Follow-up questions

What is a provider state?

A provider state is a named precondition for an interaction, such as 'customer 42 exists' or 'customer 42 is blocked'. The consumer names it in the contract, and the provider's verification sets it up before replaying the request, for example by inserting a row or configuring a mocked repository. It lets one endpoint have several contracts for different situations.

Can contract testing cover Kafka messages?

Yes. Both tools support message contracts. With Pact, a consumer test describes the message it expects to receive and the provider verifies that its producer creates a matching message. With Spring Cloud Contract, a contract can describe a message sent to a destination, and the generated test triggers the provider to send it and checks the result.

Is contract testing the same as schema validation against OpenAPI?

No, though they help each other. An OpenAPI description says what the provider offers; a consumer contract says what one consumer actually uses. Validating against OpenAPI catches responses that break the published schema, but it cannot tell a provider which fields are safe to remove. Contracts can.

7Conclusion

In this answer, we saw that consumer-driven contract testing turns the interactions between services into executable agreements: consumers state the requests they make and the response parts they rely on, and providers verify them in their own pipelines. Pact generates pact files from consumer tests, shares them through a broker and verifies them on the provider, with can-i-deploy as a deployment gate. Spring Cloud Contract keeps DSL contracts in the provider's repository, generating provider tests and WireMock stubs for consumers. Both catch breaking API changes without deploying everything together, while leaving business logic, resilience and real wiring to other tests.

What to say in the interviewAbout 90 seconds aloud

Open with

Consumer-driven contract testing means each consumer writes down the requests it makes and only the parts of the response it relies on, and the provider's build verifies that it still satisfies all of those contracts.

Then, if they want more

So if the provider renames a field that some consumer uses, its own pipeline fails before deployment and it knows which consumer would break, and it's free to change anything no consumer uses. The two main tools differ in workflow.

With Pact, the consumer writes a test against a Pact mock server, describing the interaction with matchers like stringType, and that test generates a pact JSON file. It's published to a Pact Broker.

The provider's verification test pulls the pacts, sets up provider states like customer 42 exists, replays the requests against the real code and publishes the result. can-i-deploy then checks against the broker whether a version is compatible with what's deployed.

Spring Cloud Contract usually keeps contracts in the provider's repository, written in Groovy, YAML or Kotlin, with consumers proposing them by pull request. The build generates provider tests, often with RestAssuredMockMvc, and also WireMock stubs packaged as a stubs jar that consumers run with Stub Runner. I'd pick Pact for multi-language organisations and Spring Cloud Contract for Spring-heavy ones.

Either way, contracts check interface shape, not business logic, resilience or real deployed wiring.

About this answer

Last updated
12 November 2026
Versions it names
JUnit 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is consumer-driven contract testing, and how do Pact and Spring Cloud Contract...": https://scrivio-interview-library.netlify.app/java/test-strategy/63f4fd3d28/