0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Villa_7
  • 508
  • 4
  • 14
  • Do you need to match the schema or the contents ? which of these is your requirement ? – Wilfred Clement Jul 23 '20 at 14:04
  • @WilfredClement I don't care about contents, so need to match the schema only. I Found a way of matching with json schema file, but it's not suitable as I need to match with POJO – Villa_7 Jul 23 '20 at 15:42

1 Answers1

2

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.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • Could you please tell what did you mean here - "And then compare the object to your golden one as usual." ? – Villa_7 Jul 23 '20 at 11:49
  • You have object A and object B. The former one is what you expect to get, the latter one is what you have received from the service. Now you can use any appropriate way to test them for equality (for example `equals() ` method) – Alexey R. Jul 23 '20 at 12:17
  • equals() method will check the equality of field values for both objects, doesn't it? Field values are not my goal, I just want to compare two object whether they have the same fields and types of fields. – Villa_7 Jul 23 '20 at 12:30
  • If the returned object would not match the schema of your class then you will get the exception. – Alexey R. Jul 23 '20 at 12:34
  • I tried to code as you mentioned above, but I'm getting assertion failure as that's two different objects (links to objects) – Villa_7 Jul 23 '20 at 15:43
  • 1
    If you're talking about `equals` you need to override that method since the default implementation would return `false` for two different instances. You should also override hashcode if you're going to use your objects with hasset or hashtree or hashsomething. IDEs usually can auto-generate equals and hashcode methods. – Alexey R. Jul 23 '20 at 15:56