liveBook · Manning

Pact JUnit 5 package

As explained in Contract Testing in Action

The Pact JUnit 5 package provides JUnit 5 integration for defining and executing Pact consumer contract tests.

A mobile client can test its API expectations without calling a live movie service. The Pact JUnit 5 package integrates Pact setup with JUnit Jupiter: annotating a test class with PactConsumerTestExt starts a mock server and checks that required test elements are present. section 5.2

Use it for consumer logic tests, alongside the Android app’s test folder. A @Pact function identifies the provider and consumer, then returns a V4Pact contract whose interactions describe the request and expected response. section 5.2

Movie API consumer test

A MoviesApiPactTest uses @ExtendWith(PactConsumerTestExt::class) and PER_CLASS lifecycle. Its contract expects GET /movies when “a movie exists,” then returns 200, an application/json header, and an array of movie id, name, and year fields. A @PactTestFor test receives MockServer, points MovieClient at its URL, calls getMovies(), and asserts a nonempty response. section 5.2

Figure 5.6 Pact interaction contains the mobile app’s actual request intercepted and the expected response returned from the mock server. Finally, the interactions are converted to a Pact file.
figure

Follow the interaction from the client request through the mock-server response to conversion into a Pact file. section 5.2

How Contract Testing in Action covers it

How deep it goes
Walks through extension setup, contract construction, mock-server injection, and a client assertion; provider-side verification is not shown.
Examples
Uses a MoviesAndroidApp consumer requesting GET /movies from MoviesAPI and asserting a nonempty movie response.
What it assumes
Assumes Android logic-test placement plus Kotlin and JUnit Jupiter annotation usage.
What you can do after
Apply it via Pact JUnit 5 annotations and MockServer to test a consumer client against a defined Pact interaction.
How it connects
Connects JUnit Jupiter, PactConsumerTestExt, V4Pact, MockServer, HTTP requests, and JSON response bodies.

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.