Skip to content
Advertisement

Rest-Assured – How to validate response body with POJO

I’m freshman in Rest-Assured. I have a simple test which gets response body and I want to validate whether the response body matches with my POJO class.

Here is my test:

  @Test
  public void getMySmartPlansList() {
    MySPList mysp = new MySPList();

      given().log().all().spec(getReqSpec())
      .get(Endpoints.getMY_SP())
      .then().assertThat().statusCode(200).body("first_page_url", equalTo(mysp.getFirst_page_url()));
    System.out.println("SUCCESS");
  }

Here is my POJO class:

package com.payloads;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import java.util.List;

@Getter
public class MySPList {
  private int current_page;
  private List<MySPObject> data;
  private String first_page_url = "/?page=1";
  private int from;
  private int last_page;
  private String last_page_url;
  @JsonIgnore private String next_page_url; ////
  private String path;
  private int per_page;
  @JsonIgnore private String prev_page_url; ////
  private int to;
  private int total;
}

So how to validate that the response body structure is equal to my POJO class?

Thanks in advance

Advertisement

Answer

Try the approach like this:

MyPOJO myPojo = RestAssured.given()
                .get(new URL("https://YOU_URL"))
                .getBody()
                .as(MyPOJO.class);

And then compare the object to your golden one as usual.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement