Show List
Running WireMock As Standalone Server
WireMock can be used as a web server that can serve mocked response to incoming requests. In this tutorial we will see how to set up WireMock as standalone server, capture requests and response and to serve the mock response.
In order to run the WireMock server, we should have Java installed on the system. If you do not have Java installed, you can follow the steps from this tutorial
Running the WireMock server
WireMock standalone jar can be downloaded from the official page https://wiremock.org/docs/running-standalone/
Start the server by running command "java -jar jarfilename"
When the server starts it creates two folders "mappings" and "__files" in the same directory where jar was present. "mappings" folder is used to store the requests and "__files" folder can be used to store the responses.
By default the server starts on port 8080. We can change the server port by providing port number in the command as below.
Access WireMock Recorder
WireMock recorder can be accessed from the browser using the server port number http://localhost:9999/__admin/recorder/
The recorder is used to capture the requests and responses from the actual API.
Capturing Requests and Responses
For this demo, we are going to use the REST API created in the earlier tutorial. Download the code and run the Spring Boot application in the IDE. When the application is up, it can be accessed from below end point on Postman.
Now we are going to mock this request and response so that we get the same response from WireMock server by going to the end point "http://localhost:9999/students" instead of going to the real API end point "http://localhost:8080/students".
To do this, first we have to capture the request and response in WireMock. On the WireMock recorder page, enter the target url (of the real API) and click on "Record" button. "Recording" status will show as below:
On the Postman, change the end point to "http://localhost:9999" so that the request goes through WireMock server. When "Send" button is clicked, the response would display and both request and response get captured in WireMock.
Click on the stop button on the WireMock recorder. It will display the count of captured requests:
The captured stub can be found in the "mappings" folder:
Playing Back the Recorded Requests
Now we can stop the real SpringBoot API as the request and response have been captured in the WireMock server and it can use that to serve the request.
Call the endpoint with WireMock host to see the recorded response:
Leave a Comment