Working with WireMock

Pruthvi Vikram
4 min readMar 4, 2022
Picture : https://wiremock.org/

CODE URL : https://gitlab.com/vikrampruthvi5/wiremock

Idea

In this topic, we are going to see an example on how to use wiremock to test the api access functionality. Under the assumption that there is an application (python or java) that makes a call to a particular api and get the response from the API which is further used for processing.

How is the beneficial?

WireMock application creates a configurable API that mimics the original application which sends response in the form of JSON over HTTP protocol. Any developer can create a dummy response and allow it listen on a particular port like a server and serve the application.

Setup

  1. Download WireMock
  2. WireMock Setup & Frist run
  3. Configure Wiremock Mappings
  4. Develop An Application (Python in our case)
  5. Test The Application

Download WireMock. Wiremock can be downloaded as a standalone application or an extension. Here in our scenario we are setting up wiremock as a standalone application in the local. Download wiremock.jar file from downloaded the standalone JAR or https://wiremock.org/docs/running-standalone/

WireMock setup & First run. Open terminal(Mac) and go to the Downloads folder and make a new folder named wmock(can be anything).

Now move the downloaded jar file to that folder

Go to the folder and run the wiremock using java.
Prereq alert : Java must be installed. Check by running java -version command

Running WireMock for the first time

Use the ctrl+c to stop the server

Configure Wiremock Mappings. In this part, we will have to define custom responses depending on the requirement. For that stop the wiremock server using ctrl+c and run ls command

You should see two new folders created 1. __files and 2. mappings. Mappings is the folder where we define our custom responses. Change to the mappings folder using cd mappings command and create a new file called get_users.json

Use any text editor to add custom responses to the file. In my case I am using the vim editor. Other editors include sublime text, visual studio code, notepad++, etc…

Explanation for Keys and Values in above JSON

Key : request
Value : method(sub key) holds the type of request (GET or POST or etc…) and the url(sub key) holds the url path

Key : response
Value: status(sub key) to define the response code and bodyFileName(sub key) to define the path where json data resides. Usually __files folder created by Wiremock is used to store the json files which has the mock data that is needed.

As configured in the mappings, lets create the json data file that is sent back to the consumer as response.

Create a file named users_response.json and add the data

Json data stored in __files folder

Lets restart the mock server using the command

java -jar wiremock.jar --port 8080 --verbose

Restarting wiremock server

Test The Application. I am creating an application using python which makes a call to the wiremock server and get the response. Based on the response, i will be changing the action(In my case print statement)

Code

Above python code is self explanatory. When started, application provides options to the user and ask them make a selection. Upon making selection of 1, requests.get method is used to make a http call to wiremock server and prints the response.
Note: No error handing and exception handling is added to keep it simple

Test output

For more information please visit https://wiremock.org
CODE URL : https://gitlab.com/vikrampruthvi5/wiremock

Subscribe for more updates. Also, please remember that your appreciation is my fuel.

--

--