POJO implementation in API testing (Automation) using Rest Assured

09 / Sep / 2025 by Ankit Shrivastava 0 comments

Introduction:-

A straightforward Java class called POJO (Plain Old Java Object) stores data object-orientedly and is independent of frameworks. POJOs simplify, tidy up, and reuse payload handling. We can improve the readability, resilience, and maintainability of our script by implementing the POJO in API automation. Java objects are automatically converted to JSON/XML by serialization, and API responses are automatically converted into Java objects by deserialization, eliminating the need for manual parsing.

What you will learn in this blog:

  1. POJO Implementation in API Automation.
  2. Serialization/De-Serialization.
  3. Writing the test script to automate the POST API call with POJO.
  4. Writing the assertion in the deserialized API response.

Tech Stack used in this blog:

1. For this demo, we will use an open API endpoint “http://httpbin.org/post.”
2. JAVA 21.0.4.
3. Rest Assured library, version 4.5.1
4. Jackson-databind library, version 2.15.0
5. TestNG, version 7.7.1

Let’s get started with the actual implementation in the code:

For this demo, we will use an open API endpoint (http://httpbin.org/post), which uses the following data as a payload.
{

“firstname”: “Mark”,
“lastname”: “Flower”,
“gender”: “Male”,
“age”: 40,
“salary”: 500000
}
Move 1:-

First, we will create a class with the name “Employee” and declare all the payload’s fields as private variables, and following that, we will create the getter and setter for all keys
public class Employee
{

private String firstname;
private String lastname;
private String gender;
private int age;
private double salary;

public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

Move 2:- Creating the test class in which the actual test script will be written.

public class Test

{
// Import Statements for this Test class
import org.testng.Assert;
import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;


// Step 1:  Creating the object of the Employee class and setting the values in the API’s payload

Employee  ObjEmployee1 = new Employee () ;

ObjEmployee1.setFirstname(“Mark”);
ObjEmployee1.setLastname(“Flower”);
ObjEmployee1.setGender(“Male”);
ObjEmployee1.setAge(40);
ObjEmployee1.setSalary(50000);

// Step 2: Converting Employee Class Objects to JSON as a String with object mapper

ObjectMapper  Obj_ObjectMapper = new ObjectMapper ();
String EmployeeData  =  Obj_ObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(ObjEmployee1);
System.out.println(EmployeeData);

 // Step 3: Writing the script for the post call and using the converted  JSON from Step 2 as an API payload.

RequestSpecification requestSpec  = RestAssured.given();
requestSpec.baseUri(“http://httpbin.org/post”);
requestSpec.contentType(ContentType.JSON);
requestSpec.body(EmployeeData);
Response resp  = requestSpec.post();
resp.prettyPrint();
Assert.assertEquals(resp.statusCode(), 200,”Check for status code”);

// Step 4 : Convert JSON String (employeeJSON) to Class object (Employee) 

Employee ObjEmployee2 =  Obj_ObjectMapper.readValue(EmployeeData, Employee.class);

String firstName  = ObjEmployee2.getFirstname();
String lastName  = ObjEmployee2.getLastname();
int     empAge      = ObjEmployee2.getAge() ;

// Step 5 : Applying assertions to verify that the API is returning the expected data or not?

Assert.assertEquals(firstName, “Mark”);
Assert.assertEquals(lastName, “Flower”);
Assert.assertEquals(empAge, 40);
}

Conclusion:—The complexity of developing the script and running it would be significantly higher if the above-explained code were implemented without the POJO, but by utilizing the POJO, we were able to create a less complicated script. Additionally, we simplified the code’s readability and maintainability.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *