0% found this document useful (0 votes)
59 views17 pages

Serialization and Deserialization in Rest Assured 1742358604

The document explains the processes of serialization and deserialization in REST assured using POJO classes, which are simple Java classes designed to store and transfer data. Serialization converts Java objects into JSON for API requests, while deserialization converts JSON responses back into Java objects. It also highlights the need for external libraries like Jackson and Gson for these processes and outlines the steps to create POJO classes for handling JSON data.

Uploaded by

ranjeetp.pune
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views17 pages

Serialization and Deserialization in Rest Assured 1742358604

The document explains the processes of serialization and deserialization in REST assured using POJO classes, which are simple Java classes designed to store and transfer data. Serialization converts Java objects into JSON for API requests, while deserialization converts JSON responses back into Java objects. It also highlights the need for external libraries like Jackson and Gson for these processes and outlines the steps to create POJO classes for handling JSON data.

Uploaded by

ranjeetp.pune
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Serialization & Deserialization of request/responses with POJO classes –

There are two important things in rest assured –

a. Build our payload, how to construct our request payload to send to the API
b. When got response, how to parse response to scan for our actual value

It can be achieved using Serialization and Deserialization.

What is Serialization? (Java Object → JSON)

Serialization is the process of converting a Java object into a JSON (or XML) format so
it can be sent in an HTTP request body (payload).

What is a POJO Class in Java?

A POJO (Plain Old Java Object) is a simple Java class that is used to store and transfer
data.

It contains private fields, public getters/setters, and no special behavior beyond


storing data.
Steps –

a. For all the keys in our json (message) we will create a variable in the class, which is
one of the key in our json
b. For that variable we have to create two methods getters() and setters()
c. We will set the value to the key variable using setter method
d. So we need to create the class object and then call the setter method by sending
the value of the key as an argument

Note – We need to declare all the variables as private and all the methods as
public, it’s the standard for defining POJO class

Once we create java class like this with a skeleton for all our json, we have created pojo
class for our json keys

e. First we need to create java object of the entire class, after that we can access
methods present in that class with object of that class
f. By default there are no values assigned to the keys, rest assured will automatically
have the knowledge to convert the pojo class into json format.
The way it converts is, it will check what all variables are declared and creates keys
of them with no values
g. In our test we have to send the value of keys using the setter methods so when we
send a value as an argument to the setter method, then that value will be assigned
to the key which is present globally.
h. Once a value is assigned to the key, automatically that will be assigned in the json
format keys.

Advantages of Serialization –
Easy to parse and extract response (JSON/XML) values if they are wrapped as java
object.
User friendly methods can be created which makes code more readable.

Design Approach –
Java object is constructed with the support of POJO classes.
POJO classes are created based on the request/response payload.

After we set all the methods we will just send java object now rest assured will
automatically look at the java object and it will see its associated POJO class and it
will map our java object set as to this POJO class and creates json for us at runtime
and sends that json to our endpoint.
What is Deserialization? (JSON → Java Object)

Deserialization is the process of converting JSON responses back into Java objects for
easy data handling.
Note –

If our response and request is a json and if we want to apply serialization and
deserialization we need to have Jackson Library and Gson library in our class path

And if its an xml we need to have JAXB library.

These are the libraries we have to bring and add into our project build path, So restassured
alone will not do the deserialization we need to depend upon external libraries to achieve
this mechanism

Jackson Databind –

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.18.3</version>

</dependency>

Gives capability of deserialization and serialization.


This is the response of an api call and our goal is to deserialize this json response to a
java object and extract desired values out of it
Step 1 -> Create POJO classes according to the response –

GetCourseResponse (main class)


Courses (sub class)
Nested json
Deserialization using the main class object to convert back the json response into java
object and extract values out of it
Workflow explanation –

You might also like