Comparing JSON responses using JsonAssert Library - Strict Mode

 

In one of the earlier post, we studied about Comparing JSON responses using JsonAssert Library - Lenient Mode, 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 using the Strict Mode provided by the JSONAssert Library. In case of Lenient mode if the order of elements in an array are not same then still the test will pass.

In case of Strict mode if the order of elements in an array are not same then the test will fail.

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 changed the order of the elements present in the candidate array. The response provided by the API might change, so while running your test if you find any discrepancy then please use the latest response provided by the API and change the order of elements in the candidate array.
We will need to add following dependency to the pom.xml apart from the other dependencies we have added earlier. ( Mentioned in Lenient Mode blog as well )
<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 {
    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", "AIzaSyDtejOLyhH5yrzZInfoQak7SubrZEeCKvc")
            .when()
            .get()
            .asString();
    JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.STRICT);
  }
}


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 and using the Strict mode

In this case, the test will fail as expectedResponse will not match the actualResponse, since we changed have the order of elements in the candidates array which won't match with the actualResponse.

We will get output as follows where we can clearly see that since we have changed order in expected response we are getting the following assertion error. 




Comments

Popular posts from this blog

Extracting an XML Response with Rest-Assured