Rest Assured : Serialisation and De-Serialisation
In this article I have explained how to use serialisation and deserialisation to process request and response in Rest Assured. Below are sections covered-
- What is serialisation and deserialisation?
- Why to use serialisation and deserialisation in REST API automation?
- What is POJO in Java?
- Serialisation of request body into JSON
- De-Serialisation of JSON response into POJO
What is serialisation and deserialisation?
- Serialisation 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 deserialisation.
- Why serialisation is used? Application involving multiple platforms, requires architecture independence. Serialising the data structure in an architecture-independent format means preventing the problems of byte ordering, memory layout, or simply different ways of representing data structures in different programming languages.
Why to use serialisation and deserialisation in REST API automation?
To access REST service we have to transfer data between client and REST service. Using serialisation, object having request details can be serialised into JSON/XML format which can be handled by REST service. And using deserialisation REST response which can be in JSON/XML format can be converted back to objects which be consumed by client.
What is POJO in Java?
- What is POJO (Plain Old Java Object) class in Java?
POJO is a simple data structure that has private fields with public getter and setter, but it does not have business implementation. It is used to improve readability and reusability of a program.
POJO should not
- extend pre-specified class
- implement pre-specified interface
POJO should follow below property
- class must be public
- instance variables must be private
- must have public default constructor
- it should have getter/setter method for accessing/updating instance variables
Serialisation of POJO into JSON
1. Jackson library are used for serialisation and deserialisation. Below dependency should be added in project to work with Jackson in java.
- jackson-databind, jackson-annotations, jackson-core
2. In order to work with serialisation or deserialisation to send request or to process response, we have to create POJO class in Java.
3. Using POJO class in java and Jackson library we can serialise REST request i.e. we can convert POJO into byte of streams (JSON/XML) which will send to endpoint and get processed by service. In below example I have used Jira api’s to create issue using REST request. This request require json parameter which is having nested attribute in JSON.
Consider below points in above JSON
- summary and description are String parameter
- project is a nested attribute which contains key parameter
- issue type is nested attribute which contains name parameter
- All of above parameter are present inside attribute field.
To create POJO class for such request we need to have separate POJO classes created as below where we have separate class for each attribute section. Below is how we create POJO classes for above JSON and how it get processed.
De-Serialisation of JSON response to POJO
1. For understanding de-serialisation, Let’s consider below JSON response for library API request.
2. For above JSON response, we can create below POJO class
3. Below are how response are mapped to POJO class.
Guidelines
- Getter methods are not needed for Request POJO class
- Setter methods are not needed for Response POJO classes
- Name of instant variables in POJO classes are same s JSON attribute
Great way of explanation.. Kudos
Thanks for sharing, such a clear explanation, I was indeed looking for why do we really need this.
Thanks for sharing
Good one