How to handle Dynamic JSON Payload in RestAssured

Concluding deal with coding company
Reading Time: 3 minutes

Hello Readers!

As popularly considered, an API (Application Program Interface) is an engine of the software application. It is crucial to test the APIs as these are expected to work fine and without any deviations. In this blog, we will have an insight into RestAssured and how we can handle dynamic JSON payload with it.

What is RestAssured?

RestAssured is a testing tool that enables you to test REST APIs. It uses java libraries and can be integrated with Maven. It supports the Behavior-Driven Development approach by using given(), when() and then().

Why do we need a Dynamic JSON Payload?

Dynamic JSON Payload as the name suggest is the JSON where data is dynamically loaded. The question arises that why we actually need to dynamically pass the value to the JSON in the payload. In some business logic, there would be the requirement of unique value each time.
For example, a business logic where the mobile number should be unique to create a new user. In this case, one single JSON can not work each time and we can’t go with hardcoded values always. The effective way is here to parameterise the test case to pass the new values each time.

Dynamically build JSON payload in RestAssured

  • Create a new project. For demonstration, we are opting for IntelliJ as IDE, but you can try this with other alternatives.
  • Add the following dependencies in the pom.xml file.
<dependency>

    <groupId>io.rest-assured</groupId>

    <artifactId>rest-assured</artifactId>

    <version>5.1.1</version>

    <scope>test</scope>

</dependency>

<dependency>

    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>7.6.1</version>

    <scope>test</scope>

</dependency>

<dependency>

    <groupId>org.hamcrest</groupId>

    <artifactId>hamcrest</artifactId>

    <version>2.2</version>

</dependency>
  • Create a new class ValidateUser in the test directory.
  • Create a method and add TestNG @Test annotation before it.
public class ValidateUser {

    @Test

    public void addUser(){

    }

}
  • Create a new class and add a new static method with String type. I’ve created a method user here.
  • Return the JSON request from the created method
public class Payload {

    public static String userDetails(){

        return "REQUEST PAYLOAD";

    }

}
  • I am using the following JSON in the example:
{
  "name": "Test Restassured",

  "gender": "male",

  "email": "test@test.com",

  "status": "active"
}
  • Here, we will dynamically pass the email into a JSON payload. To implement it, we have to do the following tweaks in the userDetails method.
    • Add a parameter of String type, let’s say email
    • Remove the hardcoded email and concatenate the variable in the payload string.
public class Payload {

    public static String userDetails(String email){

        return "{\n" +

                "    \"name\": \"Test Restassured\",\n" +

                "    \"gender\": \"male\",\n" +

                "    \"email\": \""+email+"\",\n" +

                "    \"status\": \"active\"\n" +

                "}";
    }
}
  • Let’s go back to ValidateUser Class and add the following code in the addUser() method to send the request to the server.
RestAssured.baseURI="https://gorest.co.in";

  given().log().all().header("Content-Type","application/json") 
      .body(Payload.userDetails("test1@test.com")).when().post("/public/v2/users").then().
log().all
().assertThat().statusCode(200);
}

This is only useful when you have a single entity to pass in your request payload. This is not the optimal way when we have multiple data sets. To send multiple data entities, we have to follow the below steps:

Parameterization of API Tests with multiple data sets

  • Create a new method, say sendData() of return type Object[ ][ ].
  • Annotate the method with @DataProvider. DataProvider is a way to pass the parameters into the test function()
@DataProvider

    public Object[][] sendData(){

       return new Object[][] {{"Test RestAPI","test2@test.com"},{"Test 

RestAssured","test3@test.com"}};

    }
  • For better representation, we are dynamically sending the values: name and email into the test cases
  • Add parameters email and name of String type in addUser method.
@Test

public void addUser(String name,String email){

    RestAssured.baseURI="https://gorest.co.in";

    given().log().all().header("Content-Type","application/json")

            .body(Payload.userDetails(name, email)).

            when().post("/public/v2/users").then().log().all().assertThat().statusCode(200);

}

Here’s the result below executed,

RestAssured : Result Execution

That’s how can you handle Dynamic JSON in the payload. Rest Assured is a popular tool to test APIs. Code Reusablity is an attribute that withstands it against other API Testing tools.

References :
https://blog.knoldus.com/introduction-to-the-rest-assured/
https://www.toolsqa.com/rest-assured-tutorial/

Written by 

Gargi Sanadhya is a QA consultant with an experience more than a year in manual and automated testing. She is familiar with the core concepts of manual testing and automated testing using Selenium and Katalon Studio. She is always eager to acquire new tech skills and learn new & advanced concepts to advance herself. She likes to read books, watch web series and know more about space and technology.

Leave a Reply