Skip to content
Latchkey

How to Contract Test With Spring Cloud Contract

Spring Cloud Contract is producer-driven: the producer defines contracts, the plugin generates verification tests, and consumers pull the resulting stubs.

Write contracts under src/test/resources/contracts. The Maven or Gradle plugin generates base-class-driven tests for the producer and publishes a stub jar consumers use with Stub Runner.

Steps

  • Add the spring-cloud-contract plugin to the producer build.
  • Write contracts in Groovy DSL or YAML under the contracts directory.
  • Run the build in CI to generate tests and publish the stub jar.

CI build

.github/workflows/ci.yml
jobs:
  contracts:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
      - run: ./mvnw -B test
      - run: ./mvnw -B deploy -DskipTests

Gotchas

  • Spring Cloud Contract is producer-driven, the inverse of Pact consumer-driven flow; pick one direction per integration.
  • The generated tests need a configured base class that stands up the controller context.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →