Rest-Assured - 如何使用 POJO 验证响应主体

Rest-Assured - How to validate response body with POJO

我是 Rest-Assured 的新生。 我有一个获取响应主体的简单测试,我想验证响应主体是否与我的 POJO class.

匹配

这是我的测试:

  @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");
  }

这是我的 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;
}

那么如何验证响应主体结构是否等于我的 POJO class?

提前致谢

试试这样的方法:

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

然后像往常一样将对象与您的黄金对象进行比较。