nonstopio
Published in

nonstopio

Serialization and Deserialization in Rest-assured API Testing

REST-assured

Hello Readers,

We talked about API testing in the previous article and now we are going to talk about serialization and de-serialization in the Rest-Assured API test with Java. We will also look at how to use POJO classes in REST-Assured API testing.

It’s very likely that you could have a requirement that you have a POJO (Plain Old Java Object) and you need to send it to the API call. To do that we need to ‘Serialize’ it in Rest-Assured and this process is referred to as ‘Serialization’. While on the other hand, you have the API response and you would need to de-serialize it into a POJO then it’s called ‘De-Serialization’

What mean by serialization and de-serialization

Serialization is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. The opposite operation, extracting a data structure from a series of bytes, is deserialization.

Serialization and De-serialization

POJO classes[Plain old java objects]

  • An ordinary Java class that does not extends or implement properties from any technology or framework-related classes and interface is called a POJO class.
  • POJO classes are used to represent data.
  • POJO Class will contain only default constructor, private data members, and public setter and getter methods for every data member.
  • Setter methods are used to set the value to the variable.
  • Getter methods are used to get the value from the variable.
  • Basically, POJO defines an entity.

How to use POJO classes in REST-assured

Java supports Object Oriented Programming System (OOPs) which is a programming paradigm. From the full form of OOPs, we can understand that it is a programming approach that is oriented around something called “Object” and an Object can contain data (Member variables ) and mechanism (member functions) to manipulate these data.

For example — A company has many employees and there are some basic details that need to be stored in such a way that they can be accessed and manipulated easily. If we create a blueprint of an Employee which defines what are data to be stored and mechanisms to manipulate those data then our problem can be solved.

We can create a Class “Employee” ( We can give any meaningful name) and to store employee-related data we can declare fields or variables in this class. We also need to retrieve and manipulate data so we need to define some mechanism or methods to do so.

package PojoPayloads;public class Employee {

// fields to store data
public String firstName;
public String lastName;
public double salary;
public String cityName;
public boolean isMarried;
public char gender;

// Create employees with different data
public Employee(String firstName, String lastName, double salary, String cityName, boolean isMarried,
char gender) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.cityName = cityName;
this.isMarried = isMarried;
this.gender = gender;
}

// Public getter setter methods
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public boolean isMarried() {
return isMarried;
}
public void setMarried(boolean isMarried) {
this.isMarried = isMarried;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}

}

In the program, “amod” and “animesh” are ordinary Java objects or POJO.

package PojoPayloads;public class EmployeeDetails {public static void main(String[] args) {

Employee amod = new Employee("Amod", "Mahajan", 100000, "Benagluru", false, 'M');
Employee animesh = new Employee("Animesh", "Prashant", 200000, "Benagluru", true, 'M');

// Get married status of Amod
System.out.println("Is Amod married? : "+amod.isMarried());

// Amod is married now and change data
amod.setMarried(true);
System.out.println("Is Amod married now ? : "+amod.isMarried());

// Increase salary of animesh
animesh.setSalary(500000);
System.out.println("Updated salary of animesh : "+ animesh.getSalary());
}
}

POJO stands for Plain Old Java Object and it is an ordinary Java object, not a special kind. The term POJO was coined by Martin Fowler, Rebecca Parsons, and Josh MacKenzie in September 2000 when they were talking about many benefits of encoding business logic into regular java objects rather than using Entity Beans or JavaBeans.

How to make POJO classes more effectively

In order to work with serialization or deserialization to send requests or process responses, we have to create a POJO class in Java.

Using the POJO class in java and Jackson library we can serialize REST requests i.e. we can convert POJO into bytes of streams (JSON/XML) which will send to the endpoint and get processed by service. In the below example I have used Jira APIs to create issues using REST requests. This request requires a JSON parameter which is having nested attribute in JSON.

‎To create a POJO class for such requests we need to have separate POJO classes created as below where we have separate classes for each attribute section. Below is how we create POJO classes for the above JSON and how it gets processed.

I would like to hear feedback from you so please share your feedback here.

Thank you.

A Bespoke Engineering Studio

Recommended from Medium

[0] Git — My top 8

Java Object Oriented Design — Java Generic Methods and Constructors

Turnkey IoT Solution from SocketXP for Raspberry Pi Remote SSH Access

PHP Access Modifiers Example

Framework is a must for better programming

How To Create Golang REST API: Project Layout Configuration [Part 3]

Top 5 Mistakes I Made While Learning to Code in School

MoonieNFT Arcade Game Beta testing sign ups — Be the First to play MoonieNFT Arcade GAMES!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kishor Munot

Kishor Munot

Test Engineer

More from Medium

What is Selenium 4? The latest in Selenium Automation Testing for browsers

Database Testing using Robot Framework

Relative Locators in Selenium 4 : Yet another way to handle dynamic Web Tables and Pagination

The art of Cypress optimization: Boost test run time with parallelization strategy + GitLab