{"id":18875799,"url":"https://github.com/wiremock/wiremock-pact","last_synced_at":"2025-11-04T09:05:27.517Z","repository":{"id":235875773,"uuid":"791439417","full_name":"wiremock/wiremock-pact","owner":"wiremock","description":"Get requests from Wiremock and create PACT JSON.","archived":false,"fork":false,"pushed_at":"2024-09-16T15:53:34.000Z","size":230,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-09-16T19:11:35.839Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wiremock.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-24T18:09:38.000Z","updated_at":"2024-09-16T15:53:37.000Z","dependencies_parsed_at":"2024-05-09T08:25:37.160Z","dependency_job_id":"5cb87d1b-a830-4373-a359-c26cb419a470","html_url":"https://github.com/wiremock/wiremock-pact","commit_stats":null,"previous_names":["tomasbjerre/wiremock-pact","wiremock/wiremock-pact"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiremock%2Fwiremock-pact","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiremock%2Fwiremock-pact/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiremock%2Fwiremock-pact/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wiremock%2Fwiremock-pact/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wiremock","download_url":"https://codeload.github.com/wiremock/wiremock-pact/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223639348,"owners_count":17177816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T06:09:13.294Z","updated_at":"2025-10-15T12:44:45.858Z","avatar_url":"https://github.com/wiremock.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WireMock Pact\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/se.bjurr.wiremockpact/wiremock-pact/badge.svg)](https://central.sonatype.com/search?q=se.bjurr.wiremockpact)\n\nIt will get the requests from [WireMock](https://github.com/wiremock/wiremock/) and create [Pact JSON](https://docs.pact.io/) files on the filesystem. The Pact JSON can be published to a [Pactflow broker](https://test.pactflow.io/) with `curl`, see example in this readme.\n\nThis repostory contains:\n\n- `wiremock-pact-lib` - *A library that can transform WireMock [ServeEvent](https://github.com/wiremock/wiremock/blob/master/src/main/java/com/github/tomakehurst/wiremock/stubbing/ServeEvent.java):s to Pact JSON.*\n- `wiremock-pact-extension-junit5` - *A WireMock extension, and JUnit 5 extension, that is intended to ease usage of the library.*\n- `wiremock-pact-example-springboot-app` - *A SpringBoot application that shows how it can be used.*\n\n## Use case\n\nOne simple use case to quickly show what this solves. It will take the requests, exactly as the system under test is performing them, and create the Pact JSON with that information. If you are already using WireMock in your integration tests, this tool should make it very easy to produce Pact JSON.\n\n![Pact With WireMock](/docs/pact-with-wiremock.png)\n\n## Usage - Junit 5\n\nThe extension is both a WireMock extension and a JUnit 5 extension. When using [`wiremock-spring-boot`](https://wiremock.org/docs/solutions/spring-boot/) it can be configured like this in a base class of your tests:\n\n```java\nimport com.github.tomakehurst.wiremock.core.WireMockConfiguration;\nimport org.wiremock.spring.ConfigureWireMock;\nimport org.wiremock.spring.EnableWireMock;\nimport org.wiremock.spring.WireMockConfigurationCustomizer;\nimport org.junit.jupiter.api.extension.RegisterExtension;\nimport se.bjurr.wiremockpact.wiremockpactextensionjunit5.WireMockPactExtension;\nimport se.bjurr.wiremockpact.wiremockpactlib.api.WireMockPactConfig;\n\n@EnableWireMock({\n  @ConfigureWireMock(\n      name = \"wiremock-service-name\",\n      property = \"wiremock.server.url\",\n      stubLocation = \"wiremock\",\n      configurationCustomizers = {WireMockPactBaseTest.class})\n})\npublic class WireMockPactBaseTest implements WireMockConfigurationCustomizer {\n  @RegisterExtension\n  static WireMockPactExtension WIREMOCK_PACT_EXTENSION =\n      new WireMockPactExtension(\n          WireMockPactConfig.builder() //\n              .setConsumerDefaultValue(\"WireMockPactExample\") //\n              .setProviderDefaultValue(\"UnknownProvider\") //\n              .setPactJsonFolder(\"src/test/resources/pact-json\"));\n\n  @Override\n  public void customize(\n      final WireMockConfiguration configuration, final ConfigureWireMock options) {\n    configuration.extensions(WIREMOCK_PACT_EXTENSION);\n  }\n}\n```\n\n## Usage - Library\n\nIt can be used as a library.\n\n```java\npublic class ExampleTest {\n  private static WireMockServer server;\n  private static WireMockPactApi wireMockPactApi;\n\n  @BeforeAll\n  public static void beforeEach() throws IOException {\n    server = new WireMockServer();\n    server.start();\n\n    stubFor(\n        post(anyUrl())\n            .willReturn(\n                ok()\n                .withHeader(\"content-type\", \"application/json\")\n                .withBody(\"\"\"\n                {\"a\":\"b\"}\n                \"\"\"))\n            .withMetadata(\n                new Metadata(\n                    Map.of(\n                        WireMockPactMetadata.METADATA_ATTR,\n                        new WireMockPactMetadata()\n                            .setProvider(\"some-specific-provider\")))));\n\n    wireMockPactApi =\n        WireMockPactApi.create(\n            new WireMockPactConfig()\n                .setConsumerDefaultValue(\"my-service\")\n                .setProviderDefaultValue(\"unknown-service\")\n                .setPactJsonFolder(\"the/pact-json/folder\"));\n    wireMockPactApi.clearAllSaved();\n  }\n\n  @Test\n  public void testInvoke() {\n    // Do stuff that invokes WireMock...\n  }\n\n  @AfterAll\n  public static void after() {\n    for (final ServeEvent serveEvent : server.getAllServeEvents()) {\n      wireMockPactApi.addServeEvent(serveEvent);\n    }\n    // Save pact-json to folder given in WireMockPactApi\n    wireMockPactApi.saveAll();\n    server.stop();\n  }\n}\n```\n\n## Stateful Behaviour\n\nWireMock has support for [stateful behaviour](https://wiremock.org/docs/stateful-behaviour). This is picked up by this tool and used to construct the [provider states](https://docs.pact.io/getting_started/provider_states). You can also set the `providerStates` in the metadata of the mappings, see [Mappings metadata](#mappings-metadata).\n\n## Mappings metadata\n\nThis tool uses the [metadata](https://github.com/wiremock/spec/blob/main/wiremock/wiremock-admin-api/schemas/stub-mapping.yaml) of WireMock mappings when generating Pact JSON.\n\nYou can adjust any mappings file like this to specify the **provider**:\n\n```diff\n{\n  \"id\" : \"d68fb4e2-48ed-40d2-bc73-0a18f54f3ece\",\n  \"request\" : {\n    \"urlPattern\" : \"/animals/1\",\n    \"method\" : \"GET\"\n  },\n  \"response\" : {\n    \"status\" : 202\n  },\n  \"uuid\" : \"d68fb4e2-48ed-40d2-bc73-0a18f54f3ece\",\n+  \"metadata\": {\n+   \"wireMockPactSettings\": {\n+     \"provider\":\"some-other-system\"\n+   }\n+  }\n}\n```\n\nOr programmatically:\n\n```java\n    stubFor(\n        post(anyUrl())\n            .withMetadata(\n                new Metadata(\n                    Map.of(\n                        WireMockPactMetadata.METADATA_ATTR,\n                        new WireMockPactMetadata()\n                            .setProvider(\"some-specific-provider\")))));\n```\n\nYou can adjust any mappings file like this to specify the **provider states**:\n\n```diff\n{\n  \"id\" : \"d68fb4e2-48ed-40d2-bc73-0a18f54f3ece\",\n  \"request\" : {\n    \"urlPattern\" : \"/animals/1\",\n    \"method\" : \"GET\"\n  },\n  \"response\" : {\n    \"status\" : 202\n  },\n  \"uuid\" : \"d68fb4e2-48ed-40d2-bc73-0a18f54f3ece\",\n+  \"metadata\": {\n+   \"wireMockPactSettings\": {\n+     \"providerStates\": [\"state1\"]\n+   }\n+  }\n}\n```\n\nOr programmatically:\n\n```java\n    stubFor(\n        post(anyUrl())\n            .withMetadata(\n                new Metadata(\n                    Map.of(\n                        WireMockPactMetadata.METADATA_ATTR,\n                        new WireMockPactMetadata()\n                            .setProviderStatesDefaultValue(Arrays.asList(\"state1\"))))));\n```\n\n## Publishing to Pact broker\n\nPact has a [CLI tool](https://docs.pact.io/pact_broker/publishing_and_retrieving_pacts) that can be used for publishing the contracts. But it requires Ruby or Docker. If you don't have that, perhaps `curl` is an option. There is [a shell script here](https://github.com/tomasbjerre/pactflow-publish-sh) that can also be used [via NPM](https://www.npmjs.com/package/pactflow-publish-sh).\n\nYou may want to use something like [git-changelog-command-line](https://github.com/tomasbjerre/git-changelog-command-line) to get the next version.\n\nThere is a test-server at https://test.pactflow.io/ that can be accessed with user `dXfltyFMgNOFZAxr8io9wJ37iUpY42M` and password `O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1`.\n\n```sh\ncurrent_version=$(npx git-changelog-command-line \\\n  --patch-version-pattern \"^fix.*\" \\\n  --print-current-version)\ngit_hash=`git rev-parse --short HEAD`\nparticipant_version_number=\"$current_version-$git_hash\"\n\nnpx pactflow-publish-sh \\\n --username=dXfltyFMgNOFZAxr8io9wJ37iUpY42M \\\n --password=O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1 \\\n --pactflow-broker-url=https://test.pactflow.io/contracts/publish \\\n --build-url=http://whatever/ \\\n --pact-json-folder=wiremock-pact-example-springboot-app/src/test/resources/pact-json \\\n --participant-version-number=$participant_version_number\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiremock%2Fwiremock-pact","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwiremock%2Fwiremock-pact","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwiremock%2Fwiremock-pact/lists"}