loading...

Wiremock as a Web Server for Testing

jessekphillips profile image Jesse Phillips ・2 min read

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 rodolpheche / wiremock-docker

Wiremock Docker image

Wiremock Docker Build Status Docker Pulls

Wiremock standalone HTTP server Docker image

Supported tags :

Last

Complete list

Tags

The image includes

  • EXPOSE 8080 8443 : the wiremock http/https server port
  • VOLUME /home/wiremock : the wiremock data storage

How to use this image

Environment variables

  • uid : the container executor uid, useful to avoid file creation owned by root
  • JAVA_OPTS : for passing any custom options to Java e.g. -Xmx128m

Getting started

Pull latest image
docker pull rodolpheche/wiremock
Start a Wiremock container
docker run -it --rm -p 8080:8080 rodolpheche/wiremock

Access http://localhost:8080/__admin to display the mappings (empty set)

Start a Wiremock container with Wiremock arguments

To start with these Wiremock arguments : --https-port 8443 --verbose

docker run -it --rm -p 8443:8443 rodolpheche/wiremock --https-port 8443 --verbose

Access https://localhost:8443/__admin to check https working

Start record mode using host uid for file creation

In Record mode, when binding host folders…




Posted on by:

jessekphillips profile

Jesse Phillips

@jessekphillips

Senior Quality Assurance (SDET) ¶ Avid hobby D programmer ¶ Telling people what to do because I am right.

Discussion

pic
Editor guide
 

I use this for more than just frontend testing. But what I learned was that playwright and puppeteer do this and do it much better.

You would lose the support of places like browser stack to do device testing, but those tests should be targeted tests anyway, so you could still use this wiremock approach for a limited set of targeted tests.