Testing using Wiremock-Stubbing-Part3
Hi All, Welcome Back to Part 3 of Testing with wiremock. Today we will seeing how to mock and get the desired result out of of it. If you guys haven’t watched my previous blog (Part1 & Part2) please go and check that out before proceeding.
Now let’s move toward the main agenda of this blog where we will see how request matching works and compare the actual HTTP request method with the expected request method.
Request Matching:
Request matching allows us to specify certain expectations for the HTTP requests which are received by our WireMock server.
WireMock supports matching of requests to stubs and verification queries using the following attributes:
- URL
- HTTP Method
- Query parameters
- Headers
Now lets dive in to this.First Step to start with Mocking is to do static import as follow:
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
Once this is done lets start our Mock Server. I have used via Non-Junit way if you want you can use it with Junit and @rule annotation.
static WireMockServer wireMockServer = new WireMockServer(options().port(5566));
wireMockServer.start();
configureFor("localhost",5566);
As you see above i have started my Wiremock server on port 5566 and then have used start() method to up our server.
Note: When we are doing this in our local system and if we don’t write configureFor() method it will give error while executing as it don’t know the host on which wiremock is up. We have to specify that via passing localhost and port which we have used for wiremock.
Specifying the Request Type:
The following code will return us a status code 200 and a body as below.
First of all what we see here is StubFor which has a return type of StubMapping and which take argument of MappingBuilder.
Note : To make if more align with BDD we can use givenThat() instead of StubFor()
As you can see we have written get method, so the wiremock will know it is expecting a get request which a matching URL as it parameter.
Apart from get() method we have variety of other option as below:
- any() →This method of the
WireMock
class ignores the request method. - post() →This method is used for post request.
- delete() → This method is used for delete request.
- put() → This method is used for put request
All of the above method will accept Urlpattern as an argument to it.UrlPattern help in defining the url we want to pass.
Urlpattern will take many of the below method to pass the url.We can use as per our requirement. If you see i have used urlMatching() method and had passed “employee/1” so whenever mock get a request matching this path it will give us the required output we want. Methods as below.
- urlMatching() → This method will match the Url with the regex or the pattern we provide.
- urlEqualsTo() → This Method match the exact url with the expected URL
- urlPathEqualsTo() → This method match the exact URL path to expected URL Path.
- urlPathMatching() →This method will match the Url path with the regex or the pattern we provide for the expected urlPath.
- anyurl() → This method will ignore the request url .
Below is an example which shows how we can pass different method to form a request matching and once it match required output will be shown. We can pass header,cookie,queryparam, requestbody,etc. More methods regarding this can be found here.
Here addition to setting up the request parameter we can set up the response we want. In response we can set the expected status code,expected body,expected header,etc as below.
Here as you see we have set status code as 200, body as Hello there and header with content-type as Json.
Let say if we have more than one stub for a particular API then we set the priority also as below.
If there is a case where we need to pass the JSON file as the response body that also we can set as below.
If we want to remove any Stub after our work is done we can use removeStub() to remove.
Now all our setup is done let’s run our Test Case and see if we are getting the required output or not.
To check the response we are using Rest-Assured method to fetch the response and verify the status code and body.
As you can see above we are able to get the stub Response and our verification has been passed too.
So with this blog we have seen how
- How mock can be made,
- What are the different method available in request matching
- Priorities
- How to pass body/file as response body, response header
- Verification using rest-Assured
That’s all from this blog. Stay tuned for next blog in the series till then
Stay Safe! Stay Healthy !!