Testing using WireMock-Introduction-Part1
Hi All, This is Harshit !!
After procrastinating for a long time I am finally writing my first Blog. Hope you all love it. Now coming to the main agenda for the blog i.e Wiremock.
I have recently been into a project in which wiremock was used for mocking the API response.Before that I haven’t heard of this tool,but once I started using this tool for my daily scrum Testing (Automation Testing) it was fascinating. So , I started learning more about the tool,how it work and what help it can offer in our daily QA activities.In the upcoming blog I will sharing how to setup the Wiremock server and how it can help us in testing API/UI.
Before moving forward, for those who don’t know what wiremock is , it was initially created in 2011–12 by Tom Akehurst.Wiremock is a tool/simulator for HTTP based API which capture the API request and send back.Wiremock helps us in below scenario:
- Support Testing of Edge cases & Failure scenario( Status code of 401,400,500,etc).
- Enables us to test our API without depending on the other API while that is still in development.
- Enable us to capture incoming Request and send the stubbed response with the matching endpoint
- We can use Wiremock in two ways either by library or via a Standalone process.
You can learn more by it by visiting to http://wiremock.org/docs/.
Let us discuss what was happening before Wiremock when we wanted to test something.
When we test on modern day pipeline of CI/CD on Jenkins,TeamCity,etc our integration test will run using the real API made by developers for the project.This has a severe dependency on API environment.What happen if our API is dependent on the other external API to fetch the data we have request and if that API is down then our Test get failed.There can other instances of testing the UI on integration Environment to see how how UI behave when some API is down or is not authorized and for this we can’t make the whole API Down and that where Wiremock comes into the play.
After Wiremock is Introduced:
As you can see from above image when we send the request to wiremock if the request is matching wiremock will return us the stubbed version which will be written by us and in return it will not depend on the other 3rd Party API or Environment and our integration Test can run smoothly till the time proper development/Environment is not provided for our Testing.This will mostly help in manipulating the environment.
In other word we can say,if we don’t want to write slow and inconsistent tests which can be run only if we are connected to the correct network, we should use WireMock.
Running wiremock on System:
There are two ways of running the Wiremock
- Standard JAR containing just WireMock:
We can download the jar from here and run the standalone Jar via below command.
java -jar wiremock-jre8-standalone-2.30.1.jar
2. Wiremock Jar plus all other dependencies.
Wiremock dependency is there with both Java7(deprecated) and Java8(recommended).
Java8
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
Java7
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.27.0</version>
<scope>test</scope>
</dependency>
Gradle Dependecies:
Java 8 : testCompile "com.github.tomakehurst:wiremock-jre8:2.30.1"
Now Let’s summarize the point we have covered in this blog.
- Wiremock can help in mimicking the response of API and send the matching response.
- It allows us for Testing of a service which is not ready or still in development
- It helps in writing faster Unit/Integration Test.
- How to download JAR or add dependency of wiremock in our Project.
Thanks All for the reading the blog and hope you liked it!!!
Will continue more of wiremock in my next blog.