What is Data Mock, Why and How to Use WireMock? (Part1)

Can Karabulut
Trendyol Tech
Published in
4 min readAug 22, 2022

Data mock, to explain briefly, is the imitation of the data/HTTP server in the development and implementation process of the project. For example, a project that has both a mobile and a web side is started. Here one team may have to wait for the backend to be written by the other. In this case, a data mock is used to avoid wasting time.

We can also move our testing processes forward by emulating APIs with multiple dependencies using Mock (even if the APIs don’t actually exist).

We used the data mock process in Trendyol Global team during our integration tests. Some APIs here had multiple dependencies and we had to emulate these APIs to run our tests. We also used a tool called WireMock for the data mock process. Also, of course, WireMock was working on Docker in our local. In order to run it on Docker, we first downloaded the WireMock image from the docker website and placed it on Docker.

Testing

No API? No Problem! It’s Mock API!

What is WireMock?

WireMock is actually a simulator with which we can emulate HTTP-based APIs. In simple terms, WireMock is a fake server that contains the necessary data and returns us the necessary/correct information. WireMock provides us with this information by keeping the required information, or fake server, in a JSON file.
As a standalone WireMock server, you can create a simple Java application with a Maven/Gradle dependency and keep it as a running process.

WireMock Setup

First of all, we need to add the WireMock tool to our test project. The way to do this is to add WireMock dependency to our POM.xml file.

For Example:

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>

Then we need to add the wireMock parameters to our docker-compose.yml file. Here we can give our own Docker File path to the part that says “Your-Path”. Since we run our tests in the pipeline, we have given the image address on the server here.

After that we create a file named WireMockConfig in the folder where our test steps are located.

As in the picture above, we enter our WireMock url and port information in our WireMock config file. The information in the WireMock URL and WireMock Port section here should be the urls of the media files in the environment file of our test.

Also, as above, if we are using a framework, we need to create a class by extending the framework core file.

As can be seen in the two pictures above, we have defined a class called “wireMockServer”. We show that the class we created on this page is also dependent on the class named “wireMockServer” above.

We used Java/Rest Assured/Cucumber in our tests. Here, we keep the data that we will mock with WireMock in the JSON file in the folder named responses.

Since we use cucumber for the paths of our JSON files, we entered the home page of our tests.

Example JSON File:

Of course, as the projects change, we should also update the data and responses we mock up.

Finally, our test will have a BaseStep. We should call WireMock inside this BaseStep class. Then we will be able to use WireMock in all the classes we extend from BaseStep.

In general, this is how we use it in data mocking and testing processes. Thank you for reading.

--

--