英文:
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("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
答案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("https://YOU_URL"))
.getBody()
.as(MyPOJO.class);
And then compare the object to your golden one as usual.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论