Wiremock as a Web Server for Testing

jessekphillips

Jesse Phillips

Posted on December 1, 2019

Wiremock as a Web Server for Testing

This requires a little bit of background. Our product is sold highly customized so we utilize a forking model for development. The UI has a clear interface to the backend (no backend rendering), there are only a few files (not a sprawling website of content).


Wiremock provides a http(s) server which will respond to a request based on matching criteria (this can be the url, header, or body content).

Wiremock exposes a rest api allowing for the mocking to be defined with the tests and posted to the server before test execution (or as part of). This keeps the test self contained, preventing the need to maintain a "test" environment.

Multithreaded testing can be challenging in this situation because the mappings can conflict with each other if the match criteria is not sufficiently unique to the test session.


Wiremock is our chosen mocking solution. We have hosted our product in a docker container with Wiremock serving the UI and the UI configured to communicate back to wiremock for all requests. This provides a self contained UI build where our selenium tests can handle and mock all UI interactions.

To add this testing into a pipeline, we needed to stand up the container and have a predictable url. This meant we have Nginx provide redirection based on project and branch. It also provide an environmental config our test solution utilizes. This is needed because we host for many projects and branches, so the container selects a random port.


To provide pre-defined mappings. Start from the docker image. You then need to copy over the mappings, and duplicate the entry point and cmd

GitHub logo wiremock / wiremock-docker

Wiremock Docker image

WireMock Docker images

Main Nightly Docker Pulls a

The official Docker image for WireMock Standalone deployments It includes WireMock for Java under the hood, and fully compatible with its features You can learn more about WireMock standalone on the WireMock docs site.

Quick Start

In a temporary directory, checkout the demo repository, pull the Docker image, and start the WireMock instance.

docker pull wiremock/wiremock:latest
git clone https://github.com/wiremock/wiremock-docker.git
docker run -it --rm \
  -p 8080:8080 \
  -v $PWD/wiremock-docker/samples/hello/stubs:/home/wiremock \
  wiremock/wiremock:latest
Enter fullscreen mode Exit fullscreen mode

You will get a CLI output like this one:

██     ██ ██ ██████  ███████ ███    ███  ██████   ██████ ██   ██
██     ██ ██ ██   ██ ██      ████  ████ ██    ██ ██      ██  ██
██  █  ██ ██ ██████  █████   ██ ████ ██ ██    ██ ██      █████
██ ███ ██ ██ ██   ██ ██      ██  ██  ██ ██    ██ ██      ██  ██
 ███ ███  ██ ██   ██ ███████ ██      ██  ██████   ██████ ██   ██
----------------------------------------------------------------
|
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
jessekphillips
Jesse Phillips

Posted on December 1, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related