POJO class Complex Request Payload - Notes By ShariqSP
Handling Complex Request Payload Using POJO Classes in RestAssured
In this section, we will explain how to handle complex JSON request payloads using POJO (Plain Old Java Object) classes with RestAssured. This approach involves creating Java classes that map to the structure of the JSON payload, allowing you to easily pass the data to RestAssured without manually constructing JSON strings or files.
Steps to Handle Complex Request Payload Using POJO Classes
- Create POJO Classes: Define Java classes that represent the structure of the JSON request. Use fields that correspond to the keys in the JSON, and include getters and setters for each field.
- Instantiate POJO Objects: Create instances of the POJO classes and set the values for the fields, representing the data you wish to send.
- Pass the POJO Object to RestAssured: Pass the POJO object to RestAssured's
.body()
method. RestAssured automatically serializes the POJO to JSON format for the request. - Send the POST Request: Use the
post()
method to send the request with the POJO object as the body. - Assert the Response Status: Ensure the request was successful by asserting the response status code.
Example Code
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
public class ApiTest {
public static void main(String[] args) {
// Create POJO instances
MobileItem mobileItem = new MobileItem();
mobileItem.setBrand("Samsung");
mobileItem.setModel("Galaxy S23 Ultra");
mobileItem.setPrice(1199.99);
mobileItem.setCurrency("USD");
Specifications specifications = new Specifications();
specifications.setProcessor("Snapdragon 8 Gen 2");
specifications.setRam("12GB");
specifications.setStorage("256GB");
specifications.setRearCamera("200MP");
specifications.setFrontCamera("40MP");
specifications.setBattery("5000");
specifications.setOs("Android 13");
mobileItem.setSpecifications(specifications);
Warranty warranty = new Warranty();
warranty.setCoverage("Manufacturer");
warranty.setDuration(2);
mobileItem.setWarranty(warranty);
Store store1 = new Store();
store1.setStore("Best Buy");
store1.setStock(25);
Store store2 = new Store();
store2.setStore("Amazon");
store2.setStock(50);
mobileItem.setAvailability(Arrays.asList(store1, store2));
Company company = new Company();
company.setName("TechCorp");
company.setLocation("New York");
company.setPhone("+1234567890");
company.setEmail("contact@techcorp.com");
company.setLastUpdated("2024-11-25T07:17:36.204Z");
company.setMobileItems(Arrays.asList(mobileItem));
// Send POST request with POJO object as the body
RestAssured.given()
.header("Content-Type", "application/json")
.body(company) // Pass the POJO object here
.post("http://apitesting.shariqsp.com:8080/api/companies")
.then()
.assertThat()
.statusCode(200); // Assert that the response status code is 200
}
}
Explanation
- POJO Classes: In this example, we have POJO classes such as
Company
,MobileItem
,Specifications
,Warranty
, andStore
. Each of these classes represents part of the JSON structure we want to send. - Instantiating POJOs: We create instances of the POJO classes and set values using the corresponding setters (e.g.,
company.setName("TechCorp")
). - Body as POJO: The
.body(company)
method automatically converts theCompany
object to JSON format before sending the request to the API. - Sending the Request: The
post("http://apitesting.shariqsp.com:8080/api/companies")
sends the POST request to the specified API endpoint with the POJO object as the request body. - Response Assertion: The
.statusCode(200)
checks if the response status code is 200, indicating the request was successful.
POJO Class Structure
Below is the structure of the Company
POJO class along with its nested classes:
public class Company {
private String name;
private String location;
private String phone;
private String email;
private String lastUpdated;
private List mobileItems;
// Getters and Setters
}
public class MobileItem {
private String brand;
private String model;
private double price;
private String currency;
private Specifications specifications;
private Warranty warranty;
private List availability;
// Getters and Setters
}
public class Specifications {
private String processor;
private String ram;
private String storage;
private String rearCamera;
private String frontCamera;
private String battery;
private String os;
// Getters and Setters
}
public class Warranty {
private String coverage;
private int duration;
// Getters and Setters
}
public class Store {
private String store;
private int stock;
// Getters and Setters
}
Advantages of Using POJO Classes
- Type Safety: POJOs provide compile-time type checking, ensuring that the structure of the data is consistent with the expected format.
- Easy Serialization: RestAssured can automatically serialize POJO objects to JSON, making it easy to send complex data without manually converting it to JSON.
- Maintainability: POJOs make it easier to maintain and modify the structure of the payload, as you can directly modify the Java classes.
- Code Readability: POJOs improve code readability and make it easier to understand the data structure being sent.
Conclusion
Using POJO classes to handle complex request payloads in RestAssured is a powerful and efficient way to send structured data to an API. By creating Java objects that mirror the structure of the JSON, you eliminate the need to manually construct JSON strings or files. This approach enhances code maintainability, readability, and overall ease of use, making it ideal for handling complex API requests in automated tests.