A production-style Consumer-Driven Contract Testing (CDCT) demo using Pact JVM.
- The consumer defines the HTTP contract (Pact) by testing against a mock server.
- The provider verifies it against the real Spring Boot API.
- Contracts are automatically copied consumer → provider during the build.
- Consumer-driven contracts: the contract is owned by the consumer test suite.
- Provider verification: the provider test suite verifies every interaction from the Pact file.
- Multiple scenarios in a single contract:
GET /api/users/1→ 200 OK + JSON bodyGET /api/users/999→ 404 Not Found
- Build-time wiring:
- running
:provider:testimplicitly runs:consumer:testfirst - generated pact files are copied into
provider/build/pacts
- running
| Module | Purpose |
|---|---|
consumer |
Defines contracts (Pact consumer tests) and contains the API client (UserClient) |
provider |
Spring Boot API + Pact provider verification tests |
Run all tests (consumer + provider verification):
./gradlew testRun only consumer contract tests (generates pact files in consumer/build/pacts):
./gradlew :consumer:testRun only provider verification (will still generate/copy pacts automatically):
./gradlew :provider:testflowchart LR
subgraph Consumer
CT[Consumer Pact tests]
UC[UserClient WebClient]
end
subgraph Provider
API[Spring Boot REST API]
VT[Provider verification tests]
end
CT -->|starts mock server| MS[(Pact Mock Server)]
UC -->|HTTP calls| MS
CT -->|writes pact JSON| P[(consumer/build/pacts)]
P -->|Gradle copy| PB[(provider/build/pacts)]
VT -->|verifies| API
- Language: Java 21
- Build: Gradle (Kotlin DSL)
- Framework: Spring Boot 3 (provider), Spring WebClient (consumer)
- Contract testing: Pact JVM (consumer + provider)
- Testing: JUnit 5, Spring Boot Test
- Pact files are generated by the consumer tests and treated as build artifacts.
- Provider state handlers are implemented via
@State(...)methods in the provider verification test.
MIT. See LICENSE.