Comparing JSON responses using JsonAssert Library - Lenient Mode
In one of the earlier post, we studied about Extracting a JSON Response with RestAssured, before following this tutorial, I would recommend reading the previous blogs.
I would assume that everyone following this tutorial has knowledge of Java and TestNG. If you need help then please follow tutorials on Java and TestNG.
In this post, we will learn about comparing JSON Responses using JSONAssert. Once the response is extracted we will store it into Response class and later verify the entire response using a single assert statement instead of comparing specific attributes.
The disadvantage of directly comparing the response as string without JSONAssert is as follows:
- If we compare two jsons by simply comparing the json string then it fails to let us know where exactly the comparison has failed. It will also mark json as failed if the order of array elements in json is different.
However if use JSONAssert Library, it will let us know the exact difference where the comparison between to json string has failed as well as not mark the test as failed if the order of array elements in an array is different if the comparator mode is lenient and if kept as strict then it mark test as failed if the order is different.
We will be using findplacefromtext Google Map API. Follow the create API Key Article for generating your key.
Now let's look at an example where we will search for a particular Railway Station in Mumbai City and then assert the expected response present in expectedResponse.txt file to the actual response using JsonAssert Library.
We will need to add following dependency to the pom.xml apart from the other dependencies we have added earlier.
<dependency> <groupId>org.skyscreamer</groupId> <artifactId>jsonassert</artifactId> <version>1.5.0</version> <scope>test</scope> </dependency>
import static io.restassured.RestAssured.basePath; import static io.restassured.RestAssured.baseURI; import static io.restassured.RestAssured.given; import static java.nio.file.Files.readAllBytes; import static java.nio.file.Paths.get; import java.io.IOException; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class JSONAssertTest { @BeforeClass public void setBaseUri() { baseURI = "https://maps.googleapis.com"; basePath = "/maps/api/place/findplacefromtext/json"; } @Test public void test01() throws IOException { /* * expectedResponse.txt file contains the expected response *You will need to provide path to that file */ var expectedResponse = new String(readAllBytes(get("src/test/java/expectedResponse.txt"))); var actualResponse = given() .param("input", "Chhatrapati Shivaji Maharaj International Airport") .param("inputtype","textquery") .param("fields", "formatted_address,name,rating,opening_hours,geometry") .param("key", "xyz")
.when().get() .asString(); JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.LENIENT); } }
In setBaseUri method, we are simply setting the default URI to use in all of our tests and also setting the basePath of the API.
Now when we write out a test that calls an API (as we will do in the next step), we don’t have to type in the basePath in the get() call.
In test01 method, we are using the given - when - then format.
We start with given method and specify input, inputtype, fields and key parameter using the param method then we use the when method and use the get method to call findplacefromtext API.
Remember that we set the defaults for the base URI and base path , so the full address of the API that is actually being called is as follows:
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input="Chhatrapati Shivaji Maharaj International Airport"&inputtype="textquery"&fields="formatted_address,name,rating,opening_hours,geometry"&key="xyz"
Now we store the response in actualResponse variable as a String. After the response is stored we compare it with assertEquals function of JSONAssert Library.
In this case, the test will pass as expectedResponse will match the actualResponse.
Failure Scenario
Now lets see another case where we change the expected response a bit. You can find the expected response in the expectedresponse.txt file. This file is different than the one which we used earlier. In this case we changed the name of Airport in the file from Chhatrapati Shivaji Maharaj International Airport to Chhatrapati Shivaji Maharaj Domestic Airport.
Now when we run the above test again, it will fail and it will specify exactly where the response did not match which we can view in the following output.
In the next post, we will learn to use the strict mode and see how it works.
Comments
Post a Comment