- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
https://www.tutorialspoint.com/testng/testng_parameterized_test.htm
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 NewTest.java
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 @Test(dataProvider="Title") void dataProvPayLoad(String title, String body) { //base URL RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; //input details given().header("Content-type", "application/json") //adding post method with parameterization from data provider .body(PayLoad.postBody(title, body)). when().post("/posts").then() //verify status code as 201 .assertThat().statusCode(201); } //data provider method @DataProvider(name="Title") public Object[][] getData() { //multi-dimension element collection with two data sets return new Object[][] {{"Cypress","JavaScript"},{"Selenium","Python"}}; } }
Code Implementation in PayLoad.java
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; } }
Output
- Related Articles
- How to Add Cucumber Layer on Top of REST-assured API Tests?
- How to use TestNG data providers for parameterization in Rest Assured?
- Uploading file to S3 using Rest Assured multipart.
- How to create a project with Cucumber and Rest Assured dependencies?
- How to verify a JSON response body using Assertions in Rest Assured?
- How to get JSON fields(nodes) based on conditions using Rest Assured?
- How to handle static JSON in Rest Assured?
- How to validate XML response in Rest Assured?
- What is Rest Assured?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to iterate through and access the JSON array elements using Rest Assured?
- How to verify JSON response headers in Rest Assured?
- How to use the then method in Rest Assured?
- How to use Assertion in response in Rest Assured?
- How to update the value of a field in a request using Rest Assured?

Advertisements