How to parameterize tests with multiple data sets using Rest Assured?

We can parameterize tests with multiple data sets using Rest Assured. Using data providers we can execute a single test case in multiple runs. To know more about TestNG data providers visit the below link −

This technique can be used for dynamic payloads. For this, we shall create a Java class containing the payload.

Then in the second Java class (having the implementation of the POST request), we shall pass the dynamic fields of the payload as parameters to the request body.

Please find the project structure for the implementation below.

Code Implementation in

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
public class NewTest {

   //data provider annotation
   void dataProvPayLoad(String title, String body) {

      //base URL
      RestAssured.baseURI = "";

      //input details
      given().header("Content-type", "application/json")

      //adding post method with parameterization from data provider
      .body(PayLoad.postBody(title, body)).

      //verify status code as 201
   //data provider method
      public Object[][] getData()
      //multi-dimension element collection with two data sets
      return new Object[][]

Code Implementation in

public class PayLoad {
public static String postBody(String title, String body) {

   //request payload
   String b = "{
" +       //Parameterizing title and body fields       "\"title\": \"" +title+ " \",
" +          "\"body\": \"" +body+ " \",
" +          " \"userId\": \"34\"
}";             return b;    } }