New Module in WireMock Cloud: Contract Testing for Mock APIs

Dan Perovich
WireMock Head of Sales Engineering & Customer Success
March 1, 2024

What you need to know:

  • Mock APIs are meant to accurately simulate their real-world counterparts. In WireMock Cloud, this can be accomplished by uploading an OpenAPI spec, as well as using features such as recording or dynamic response templating. 
  • Over time drift can occur due to evolving requirements or changes that occur during the development lifecycle.
  • The new contract testing module in WireMock Cloud allows you to continuously validate your mock API and the live API against the OpenAPI spec. 
  • Use this feature to automatically detect and deal with drift and keep your mocks closely in sync with the actual APIs you’re developing against.

The Details

Why Mocks Drift

APIs change over time, sometimes very rapidly. Mocks are created and are correct at the point a consuming team needs them but might not be updated as the real API evolves.

Drift can cause challenges for API consumers who expect certain functionality, might lead to false positives during integration testing, or make it difficult to move from staging and testing to production. Luckily, our new module solves this issue for you, and makes it easy to ensure that your mocks, specs, and live APIs stay in close sync at all times.

WireMock’s recording and OpenAPI to mock generation features can often help with this by automatically updating the mock API to match reality.

How WireMock Cloud Solves API Drift

WireMock Cloud detects drift by applying a form of contract testing to your mock APIs. Contract testing is a technique for validating that two implementations of an API (such as a mock and a real API) both comply with the same predefined contract.

In this case the contract is an OpenAPI / Swagger specification that defines the expected behavior of the API - available endpoints, request parameters, response codes, payload schemas, etc. WireMock Cloud captures HTTP traffic to the mock API and validates it against the OpenAPI specification, reporting in detail any deviations observed.

This has the effect of verifying the correctness of both the client code making the requests and the mock API - errors in the request indicate a discrepancy between the spec and the client code’s model of the API, while errors in the response indicate a discrepancy between the mock API and the spec.

Here’s how it works:

Start from an OpenAPI spec:

The OpenAPI spec is the ‘source of truth’ which we will validate our API against. We expect both the mock API and the live API to return responses that are in line with the specification.

Soft validation:

In this mode, requests/responses are checked against the OpenAPI contract, with drift warnings reported to the user upon mismatches.

Hard validation:

If this mode is enabled, drift errors will block requests and return 400 errors rather than match incorrect stubs.

By leveraging validation, WireMock Cloud allows teams to align on API contracts and gain alerts on any divergence between implementations vs specifications. This saves considerable time detecting and troubleshooting drift issues before they create downstream problems.

Watch Dan Perovich, WireMock Head of Sales Engineering demo the API Validation feature via WireMock Cloud Academy.


Latest posts

Have More Questions?