liveBook · Manning

Pact specification

As explained in Contract Testing in Action

Version 4 of the Pact contract specification, defining structures for consumers, providers, interactions, and metadata.

A Pact specification is the JSON contract format produced by consumer contract tests. It records who the consumer and provider are, the interactions they agree on, and metadata describing the generated file and implementation. In Pact-JVM, V4Pact represents version 4 of this format. section 5.2 section 5.5

For an HTTP API, an interaction is a test scenario: the consumer declares any required provider state, the request it will send, and the response it expects. The contract can specify URL paths, query parameters, headers, and body objects; matchers allow dynamic values to vary without abandoning the expected shape. section 5.2 section 5.5

A V4 Pact file organizes a consumer-provider agreement into four main parts:

  • consumer and provider — the named parties linked by the contract. section 5.2
  • interactions — request and expected-response scenarios for the provider. section 5.2
  • metadata — format-version and implementation details, such as pact-jvm. section 5.2

Movie API interaction

An Android movie client can define a GET /movies interaction with the provider state “a movie exists.” Its expected response is HTTP 200, an application/json content type, and an array whose movie id, name, and year fields are type-matched. Completing the test converts the interaction into a V4 Pact file. section 5.2

Pact files also represent message contracts. A generated event contract names its consumer and event producer, then records message contents, a description, matching rules, message metadata such as content type and Kafka topic, and file metadata. After generation, the contract can be published to Pact Broker for provider verification. section 8.5 section 11.2.3

Listing 8.16 Event movie Pact, part 2
"messages": [
    {
      "contents": {
        "name": "The World's End",
        "year": "2013"
      },
      "description": "a movie add event",
      "matchingRules": {
        "body": {
          "$.name": {
            "combine": "AND",
            "matchers": [
              {
                "match": "type"
              }
            ]
          },
          "$.year": {
            "combine": "AND",
            "matchers": [
              {
                "match": "type"
              }
            ]
          }
        }
      },
      "metadata": {
        "contentType": "application/json",
        "kafka_topic": "movies"
      }
    }
  ]

Inspect a generated message entry to see how event contents, matching rules, and transport metadata are serialized in a Pact JSON file. section 8.5

How Contract Testing in Action covers it

How deep it goes
Defines V4 file parts and walks through HTTP interaction construction, while showing generated event JSON fields.
Examples
Uses a GET /movies response contract and a movie-add event containing message matchers and Kafka metadata.
What you can do after
Build it from scratch by defining consumer-provider interactions and generating a Pact file for publication.
How it connects
Connects consumer-driven contract testing with Pact Broker, provider verification, matchers, mock servers, and event-driven architecture.

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.