REST API mocking with Wiremock

Probably every developer or tester have used mocks at least once in their daily professional work. Functionality mocking is an excellent way to improve development process of integrated systems production, or testing heavy dependent application functionalities. With the growth of popularity of REST webservices, API mocking is becoming hot topic.

In this article I would like to introduce a simple getting-started tutorial of setting basic standalone REST API mock server with Wiremock on your local machine. Wiremock is a simple library written in Java for mocking web services.


To run standalone wiremock server, download jar from here and run:

$ java -jar wiremock-1.55-standalone.jar

you should see:


This means that wiremock has started an empty mock server on localhost on port 8080. After you navigate to http://localhost:8080/__admin/ in your browser, you should get empty mappings entity:


You can also change default port by adding –port parameter:

$ java -jar wiremock-1.55-standalone.jar –port 9090

Another important feature is set verbose logging to stdout:

$ java -jar wiremock-1.55-standalone.jar –verbose

Adding mappings

Next step is to add a request and response which we want to mock. Let us assume that our task is to mock API consisting of two methods:

First method:

  • request:

Screen Shot 2015-08-05 at 21.27.28

  • response:

Screen Shot 2015-08-05 at 21.33.32

Second method:

  • request:

Screen Shot 2015-08-05 at 21.34.28

  • response:

Screen Shot 2015-08-05 at 21.35.02

You may have noticed that wiremock had already generated two empty folders in it’s home directory. To add new mapping to our mock, create new json file getUser.json in mappings folder with the following content:

Above example should be pretty straightforward. Firstly, in request section, you define the URI under which you want to mock response, and the type of HTTP method. Then, in the response section, you specify HTTP status, headers and response body. If you want to use response body in json, remember to escape necessary characters. Nothing stands in our way to and some parametrization to the url query params. You can use regular expressions for that:

“urlPattern”: “/user/[0-9]+”,
“method”: “GET”

After creating a new mapping file, restart wiremock jar and you’re ready to go. You can test your mock with some HTTP client like Postman or with use of simple curl:


…and the same from Postman client:


Our first method mock is ready. Lets move to second method, which use POST request. The main difference here is that we have to define request body content in mock definition. Full snippet below:

Immediately apparent this code differs from the previous one. Main difference is method type, which is POST obviously, and bodyPatterns section, where we specify pattern for body request. You can also use here regular expressions, or apply some other method, like matches, doesNotMatch, etc:

“urlPattern”: “/addNewUser”,
“method”: “POST”,
“bodyPatterns” : [
{“matches” : “.*status=ok.*”},
{“doesNotMatch” : “.*!@#$%.*”},

Of course it’s just an basic example of usage, for complete reference please visit official documentation. Let’s restart wiremock server and test our new method mock with curl (or you can perform similar test with Postman client again):


Our second method is good to go! At this point, when you have fully functional standalone mock, you can run wiremock server locally and create automated webservice tests, while developers create real implementation. You can also use mocks of your application external dependencies, and then deploy your project on external host and test your system flow dependency without integration issues.

Feeding on the go

Wiremock standalone server have great feature of feeding your server with new mappings without creating new configuration files or restarting server. If you want to add on quickly new method mock to your server, but you do not necessarily want to store this as a new mapping in your configuration files, you can just add new method mock by sending it with curl to your wiremock server. Here’s an example of adding simple ping-pong health check to your mock server:

curl -X POST –data ‘{ “request”: { “url”: “/ping”, “method”: “GET” }, “response”: { “status”: 200, “body”: “pong!\n” }}’ http://localhost:8080/__admin/mappings/new

this request adds new GET method to wiremock with /ping endpoint. Mock should respond as follows:


Obviously your new /ping method will disappear after server restart. To add it permanently you have to add new entry in mappings files.

Continue reading

If you want to continue reading and expand your knowledge in area of  REST and microservices, I recommend you these books:

  • Building Microservices – one of the most important books for me, everything you want to know about microservices is here
  • RESTful Web APIs – another great book about REST architecture. Lots of practical knowledge about designing and consuming RESTful APIs


Mocking in today’s software development isn’t only a handy feature. It’s core practice, that every programmer and test automation developer should be familiar with. Wiremock is excellent and super fast tool for setting mocks. In this article I cover only standalone aspect of it, but wiremock has much more to offer, so I encourage you to visit official documentation. I will cover also other use cases of wiremock in future posts. If you have any questions, feel free to leave a comment.