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

huangapple go评论85阅读模式
英文:

Rest-Assured - How to validate response body with POJO

问题

以下是翻译好的部分:

我是Rest-Assured的新手。
我有一个简单的测试,用于获取响应主体,我想验证响应主体是否与我的POJO类匹配。

以下是我的测试:

@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类:

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类相等呢?

提前致谢。

英文:

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(&quot;first_page_url&quot;, equalTo(mysp.getFirst_page_url()));
    System.out.println(&quot;SUCCESS&quot;);
  }

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&lt;MySPObject&gt; data;
  private String first_page_url = &quot;/?page=1&quot;;
  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

答案1

得分: 2

尝试以下这种方法:

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

然后像往常一样将该对象与您的标准对象进行比较。

英文:

Try the approach like this:

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

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

huangapple
  • 本文由 发表于 2020年7月23日 19:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63053231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定