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 you need to ‘Serialize’ it in Rest-Assured and this process is referred as ‘Serialization’. While on the other hand, you have the API response and you would need to ‘Deserialize’ it into a POJO then it’s called ‘De-Serialization’. Now, In this tutorial, we will learn“How to perform Serialization and De-serialization in Rest Assured?”.

Simple representation of Serialization and De-serialization using RESTAssured...!!! Share on X

 

In this tutorial, we are going to cover the below topics:

  1. Serialization of POJO into a JSON Request Body Object
  2. De-Serialization of API Response to a POJO

 

But, first let’s understand what are these concepts ‘Serialization and Deserialization’ actually mean in Java.  

What is Serialization and Deserialization?

Serialization is a process of conversion of the Instance of a Class (state of the Java object) to a byte stream. Then, this serialized object or we say this Byte steam can be stored in files or external sources and can be transferred over networks.  While on the other hand, Deserialization is simply the reverse of Serialization meaning converting the byte stream back to the Java object.

To achieve Serialization, a class needs to implement Serializable Interface and such class are actually Java Beans or say POJO (Plain Old Java Object). So, basically Serialization is the process of Converting a POJO to a JSON object and converting a JSON object back to POJO is called Deserialization.

 

Let’s begin:

1. Serialization of POJO into a JSON Request Body Object

Let’s take an example, Here we are using a ‘Student’ class as a POJO which is holding some basic attributes like name, batch, and roll number.

Student.java (POJO)

 

Now our POJO is ready to use, so we will move onto our main step of creating a Rest Assured Test to perform the serialization bypassing this POJO to the API.

Here I am using an API that I build in my local machine to support this example which is having a POST Endpoint on which we are making a call request with the above-mentioned POJO (in the form of the object instance) in the Request body object.

As I’ve mentioned this line above “in the form of the object instance”, it means we need to create a sample object instance of Student (POJO) class and specify some values to it. Like below:

Student student = new Student(“Isha Durani”, “A”, 101);

Now, let’s create the test class:

 

When you’ll execute this above class, Rest-Assured will render the ‘Student’ object instance into the JSON formatted request body object. Like below:

That’s it. It’s that simple to serialize the Java Object (POJO) to a JSON Request object.

 

2. De-Serialization of the API Response into a POJO

As we have seen above in point number 1, we passed POJO as a Java object to the API Request. But now, we are doing the reverse by transforming the API response to a POJO Java instance.

 

Notice that “http://localhost:9091/students/1” is the GET Endpoint. Here on this Endpoint, we are looking for a student having id as 1. You can set up any rest GET API endpoint for testing purposes.

Below is the output of the execution of TestDeSerialization class:

 

That’s it. It’s that simple to de-serialize the API Response into a Java Object (POJO).

 

Simple representation of Serialization and De-serialization using RESTAssured...!!! Share on X

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak