liveBook · Manning

Pact

As explained in Contract Testing in Action

A JVM implementation of the Pact contract-testing framework for defining and verifying consumer-provider interactions.

Pact JVM is the Kotlin- and Gradle-based implementation of Pact for consumer-driven contract testing. It lets an Android consumer define expected API interactions, run them against a mock provider, and generate a contract for provider verification. This catches consumer-provider mismatches before a mobile build and API reach a shared test environment. section 5.1 section 1.4.1 section 5.0

A consumer test records a request and the response it needs rather than calling the live provider. When the test passes, Pact generates a contract containing that interaction; the contract can then be published to a broker for the provider team to verify. Pact supports HTTP and message integrations, while GraphQL requests use a GraphQLInteraction wrapper. section 1.4.1 section 4.0 section 7.6

Set up a Pact JVM consumer test by:

  • Adding the Pact Gradle plugin and the Pact JVM JUnit 5 consumer package. section 5.1.1
  • Configuring JUnit Platform test execution, logging, and Pact-file overwrite properties. section 5.1.1
  • Defining consumer expectations as interactions, then generating the consumer contract after the test succeeds. section 1.4.1
  • Publishing generated contracts to a Pact Broker so providers can access and verify them. section 4.0

Android movie search

A mobile app calls a movies API for search results. If the app changes its expected release value from a date to a year while the API still returns a date, Pact testing exposes the disagreement through the consumer contract before the APK and API are tested together. The Android implementation uses Kotlin, Gradle, pact-jvm, and JUnit 5. section 5.0 section 5.1 section 5.1.1

Listing 5.3 build.gradle dependencies
dependencies {
    ...
    testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
    testImplementation("au.com.dius.pact.consumer:junit5:4.3.15")
}

The Gradle dependency declaration shows the Pact JVM JUnit 5 consumer package alongside the JUnit Jupiter test dependency. section 5.1.1

Use Pact for integration boundaries, not unrelated business rules. Teams can adopt an existing tool such as Postman while proving the process, then migrate to a native contract-testing tool; Pact is selected here for broad language and community support. section 3.6.1

How Contract Testing in Action covers it

How deep it goes
Walks through Android dependencies, Gradle configuration, JUnit 5 setup, and consumer contract generation.
Examples
Uses an Android movie-search API whose consumer expects a year while the provider returns a date.
What it assumes
Assumes Android Studio Giraffe or later, GitHub access, Gradle, Kotlin, and JUnit 5 dependencies.
What you can do after
Apply it via Pact JVM, Gradle, and JUnit 5 to generate Android consumer contracts.
How it connects
Connects consumer-driven contract testing with mock providers, Pact Broker, GraphQL, Kafka, and CI/CD pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Contract Testing in Action. The section links point to the parts of the book it draws on.