Serialization and Deserialization in Rest Assured 1742358604
Serialization and Deserialization in Rest Assured 1742358604
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
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).
A POJO (Plain Old Java Object) is a simple Java class that is used to store and transfer
data.
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
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 –
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.3</version>
</dependency>