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
