Site Loader
Auckland, New Zealand

In this post we will discuss about Deserializing JSON response to POJO class in RestAssured.

Deserialization

  • Deserialization in here is especially for the JSON responses coming out from the API.​
  • So, essentially, we are deserializing the JSON type to the type we are intending to, in our case its POJO (Plain Old Java Object) classes

Why Deserialization with POJO ?

Using deserialization with POJO we are telling our code not to use hand coded codes or JSON strings rather a strongly typed string which are less error prone.​ The below example shows the body and response of not using an POJO class

For Example

The below code is actually an body being passed to an post request and then we are could verify the response using the jsonpath of restassured

The above body can be written in the POJO class shown below

package pojo;

public class LoginBody {

    private String email;
    private String password;

    public LoginBody() {}

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

So, the deserialisation can be applied in the above code in RestAssured as shown below

  var posts = response.getBody().as(Posts.class);
  assertThat(posts.getAuthor(), equalTo(authorName));

Here is the complete video of the above discussion

Once again, thank you very much for watching the video and reading the article, if you are interested in the complete video series, please check it out from here

Post Author: Karthik kk

Leave a Reply

Your email address will not be published. Required fields are marked *