Thanks to visit codestin.com
Credit goes to github.com

Skip to content

NullPoint3rDev/pact-consumer-driven-contracts

Repository files navigation

Pact Consumer-Driven Contract Testing (JVM)

Java Spring Boot Gradle Pact JUnit 5 WebClient

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.

What this repo demonstrates

  • 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/1200 OK + JSON body
    • GET /api/users/999404 Not Found
  • Build-time wiring:
    • running :provider:test implicitly runs :consumer:test first
    • generated pact files are copied into provider/build/pacts

Modules

Module Purpose
consumer Defines contracts (Pact consumer tests) and contains the API client (UserClient)
provider Spring Boot API + Pact provider verification tests

Quick start

Run all tests (consumer + provider verification):

./gradlew test

Run only consumer contract tests (generates pact files in consumer/build/pacts):

./gradlew :consumer:test

Run only provider verification (will still generate/copy pacts automatically):

./gradlew :provider:test

Architecture

flowchart 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
Loading

Tech stack

  • 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

Notes

  • 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.

License

MIT. See LICENSE.

About

Consumer-driven contract testing demo with Pact JVM: consumer generates pacts, provider verifies them (Spring Boot 3, Java 21)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages