Wire mock is a mocking setup integration tests. This is very simple for a mock server and configurable to return an expected response for any given request. It is entirely used during development and also importantly while integration testing while the system or service that talks to one or multiple external or internal dependencies.

Wire mock is having as the behaviour of an HTTP API and gathers the HTTP requests to send to that API. It provides us with below features:

  • It does configuration of the response returned by the HTTP API when it receives a specific request.
  • The incoming HTTP requests and code assertions for the captured HTTP requests.
  • It recognises the stubbed and/or captured HTTP requests by using request matching.
  • It organises request matchers by comparing the request URL, request method, request headers, cookies and request body with the expected values.

When Should we Use Wire mock?

We have three situations when we should use wire mock.

It has a specialised feature that uses an HTTP API that is not ready. This is a very obvious situation if we are doing Greenfield development and have to integrate our application with other systems.

Then next, we have to write unit tests for classes which use HTTP APIs. If we are writing the unit tests for a class called A which uses another class called B that uses an HTTP API, the first thing that will come to the mind is to replace the B with an mock object which isn’t good choice because we may consider that the author of the client known that it is working correctly. If the API client is jotted by us using a mock object isn’t a good choice because it doesn’t give us to verify the code can communicate with the HTTP API.

We have to write integration API or end –to-end tests for feature which use external HTTP APIs.

When we write these kinds of tests, we don’t want invoke external HTTP APIs because our tests activates an external HTTP API

  • Our tests depend from the external HTTP API-This means that our tests will fail if the external HTTP is down.It is very common that external HTTP API that doesn’t give us to initialize it into a known state before our tests that are run.
  • Our tests that are very slow than they could be-The thing that may be waiting for response from the external HTTP API takes a lot longer than getting the same response from wiremock.
  • We may not run our tests if we don’t have enough network connection-There will be a problem because there are places where we don’t necessarily have a good network connection. Some APIs block requests which don’t come from the IP address.This means that having a working network connection which may not be good.

Consider the example, where we want to test the error scenarios like an API is returning different status codes for different types of data. As the response is not under control, it is necessary to create multiple sets of data to validate different possible scenarios.

We compare both the approaches of integration testing without any mock server using an actual implementation of the external dependency and using an actual implementation of external dependency and mock server which copies responses of the requests that is received for dependency. In other case, it reduces dependency and reliance on an actual implementation of dependency and gives a lot of configuration capabilities without compromising on quality and delivery status.

Wiremock Concepts

Features of Wire mock

1. Stubbing-This will be the method which provides the configuring the HTTP response returns by the wire mock server when it receives a HTTP request. We can stub HTTP requests with wire mock by providing static given that () method of the wire mock class.

2. Verification-The wire mock server registers all requests it receives in memory as it is reset. That will make possible to request matching a specific pattern will be received and also fetch requests.

Questions

1. What is wire mock?

2. What are the features of wire mock?

Leave a Reply

Your email address will not be published. Required fields are marked *