Open In App

WireMock – Record Stub Mappings

Last Updated : 31 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

WireMock can create stub mappings from requests it has received. Wiremock provides a recorder interface to record or capture stub mappings by proxying through an actual URL. This is helpful for creating mappings automatically without having to manually configure or create mappings in the Wiremock server. Let’s demonstrate step-by-step implementation of how Record Stub Mappings work in WireMock. 

Step By Step Implementation 

Step 1: Run Your Standalone WireMock Server

Open the folder in your local machine where your WireMock JAR file is present. Open the cmd on that directory and hit the following command in order to run the JAR file.

java -jar WireMockJarFileName

 If everything is fine then as you can see in the below image our WireMock has been started successfully and the default port number is 8080. 

 

Step 2: Hit the following URL http://localhost:8080/__admin/recorder/ and the UI will look like this.

 

Step 3: Enter the URL you wish to record from in the target URL field and click the Record button. In this demonstration, we have used http://example.mocklab.io to try it out. Refer to the below image. 

 

Step 4: Now we need to make a request through WireMock to the target API so that it can be recorded. Open your Postman and for this example hit the following URL to generate the request.

GET : http://localhost:8080/recordables/123

Refer to the below image if you are stuck somewhere. 

 

You can see we get a response of 200 OK with the following response body. 

{
    "message": "Congratulations on your first recording!"
}

So as of now, we have captured one stub mapping and you can see the same message when you click on the Stop button. 

 

So now again hit the same URL http://localhost:8080/recordables/123 and you will get the response because it’s already been captured. Refer to the below image. 

 

And to see where this stub mapping has been captured you have to go to the folder where your WireMock folder is present then inside the mappings file you can see the captured JSON file as shown in the below image. 

 

And whenever you open the JSON file, you can see the details of the captured stub as the response to the above URL.

{
  "id" : "2e5c7ec2-999a-4b30-8e2a-97b00702af89",
  "name" : "recordables_123",
  "request" : {
    "url" : "/recordables/123",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "body" : "{ \"message\": \"Congratulations on your first recording!\" }",
    "headers" : {
      "Content-Type" : "application/json",
      "Matched-Stub-Id" : "bc56987f-c14f-40ae-a495-9686d8bf925e",
      "Matched-Stub-Name" : "WireMock recorder example",
      "Vary" : "Accept-Encoding, User-Agent"
    }
  },
  "uuid" : "2e5c7ec2-999a-4b30-8e2a-97b00702af89",
  "persistent" : true,
  "insertionIndex" : 5
}

So, now any request that will be made to this URL i.e http://localhost:8080/recordables/123 will be served from the stub.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads