Class PactExporter

java.lang.Object
org.mockserver.mock.pact.PactExporter

public class PactExporter extends Object
Exports MockServer response expectations as a Pact v3 consumer contract (JSON), so traffic mocked or recorded in MockServer can be published to a Pact Broker / PactFlow and used for consumer-driven contract testing.

Only expectations with an HttpResponse action and a concrete HttpRequest matcher are exported — forward, callback, and template actions have no Pact equivalent and are skipped. Matcher values are emitted as concrete example values (Pact's default exact matching).

matchingRules

Non-literal MockServer matchers are translated into a Pact v3 interaction-level matchingRules object (categories path / query / header / body, split across matchingRules.request and matchingRules.response), the inverse of the mapping in PactImporter:
  • a path/query/header value that MockServer would treat as a regex (it contains a regex metacharacter) → {"match":"regex","regex":"<value>"};
  • a NottableSchemaString param/header (a JSON-schema matcher) → {"match":"type"} (or integer/number/decimal/boolean per the schema type);
  • a JsonSchemaBody request/response body → one {"match":"type"} per top-level schema property (keyed $.field), or a single $ rule when the schema declares no properties;
  • an XPathBody request/response body → a body-category regex rule keyed by the XPath expression.
A purely literal interaction emits no matchingRules object at all, so its output is byte-identical to before this mapping existed (Pact's default is exact matching).
  • Constructor Details

    • PactExporter

      public PactExporter()
  • Method Details

    • export

      public String export(List<Expectation> expectations, String consumer, String provider)
      Parameters:
      expectations - the expectations to export (typically the active expectations)
      consumer - the consumer name (defaults to "consumer" when blank)
      provider - the provider name (defaults to "provider" when blank)
      Returns:
      the Pact contract as pretty-printed JSON