您如何使用 restassured 将整个 Json 文件与其响应进行比较?
How do you compare an entire Json file to it's response using restassured?
我正在使用几年前使用过的放心产品。这样做时,我的项目中有一个包含 Json 文件的文件夹。我将这些与 API 响应的实际结果进行了比较。解决这个问题的最佳方法是什么。我显然需要我的项目中的文件位置,并且需要将它与我的响应进行比较。有没有标准的方法来做到这一点。
原来我有这个。只是为了从 body 检查城市,但我想要整个东西。
@Test
public void GetCity() {
given().
when().
get(city).
then().
assertThat().
body(("city"), equalTo(city));
}
但我想进入下面这样的内容:
@Test
public void GetCity() {
given().
when().
get(city).
then().
assertThat().
JsonFile(("/Myjson"), equalTo(response));
}
我目前正在使用 TestNg,但我记得使用 Cucumber Scenarios,它允许我测试数据中的多个响应 table。我的问题是如何实现上述目标?
{
"id": 25,
"first_name": "Caryl",
"last_name": "Ruberry",
"email": "cruberryo@smugmug.com",
"ip_address": "222.10.201.47",
"latitude": 11.8554828,
"longitude": -86.2183907,
"city": "Dolores"
}
我从问题中了解到的是从 API 获得响应并与 JSON 文件进行比较。怎么做:
@Test
public void GetCity() {
Response response = when().
get(city).
then().
extract()
response();
}
首先,我们提取包含状态代码或响应正文等信息的 Response
对象。在这种情况下,它将是 JSON。在我们提取它之前,让我们创建一个具有 JSON 表示的 POJO:
{
"id": 25,
"first_name": "Caryl",
"last_name": "Ruberry",
"email": "cruberryo@smugmug.com",
"ip_address": "222.10.201.47",
"latitude": 11.8554828,
"longitude": -86.2183907,
"city": "Dolores"
}
上面的JSON可以用下面的class表示:
public class UserEntity {
public Long id; //id is exact name field in JSON
@JsonProperty("first_name"); //other approach
public String firstName;
public String last_name;
public String email;
public String ip_address;
public Long latitude;
public Long longitude;
public String city;
}
现在,我们可以将 JSON 响应主体转换为 class,如下所示:
@Test
public void GetCity() {
Response response = when().
get(city).
then().
extract()
response();
UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}
“$”表示 JSON 文件的根目录(第一个对象 {})。这就是 Response 被翻译成 POJO 的方式。我们可以在非常相似的事情中做到这一点
Response response = when().
get(city).
then().
extract()
response();
UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
UserEntity userEntityFile = JsonPath.from(new File("file path"));
现在您可以像这样轻松地比较它们:
assertEquals(userEntityFile.id, userEntityResponse.id);
您也可以覆盖 hashCode()
和 equals()
方法,但如果您只是在学习,那可能太多了:)
我正在使用几年前使用过的放心产品。这样做时,我的项目中有一个包含 Json 文件的文件夹。我将这些与 API 响应的实际结果进行了比较。解决这个问题的最佳方法是什么。我显然需要我的项目中的文件位置,并且需要将它与我的响应进行比较。有没有标准的方法来做到这一点。
原来我有这个。只是为了从 body 检查城市,但我想要整个东西。
@Test
public void GetCity() {
given().
when().
get(city).
then().
assertThat().
body(("city"), equalTo(city));
}
但我想进入下面这样的内容:
@Test
public void GetCity() {
given().
when().
get(city).
then().
assertThat().
JsonFile(("/Myjson"), equalTo(response));
}
我目前正在使用 TestNg,但我记得使用 Cucumber Scenarios,它允许我测试数据中的多个响应 table。我的问题是如何实现上述目标?
{
"id": 25,
"first_name": "Caryl",
"last_name": "Ruberry",
"email": "cruberryo@smugmug.com",
"ip_address": "222.10.201.47",
"latitude": 11.8554828,
"longitude": -86.2183907,
"city": "Dolores"
}
我从问题中了解到的是从 API 获得响应并与 JSON 文件进行比较。怎么做:
@Test
public void GetCity() {
Response response = when().
get(city).
then().
extract()
response();
}
首先,我们提取包含状态代码或响应正文等信息的 Response
对象。在这种情况下,它将是 JSON。在我们提取它之前,让我们创建一个具有 JSON 表示的 POJO:
{
"id": 25,
"first_name": "Caryl",
"last_name": "Ruberry",
"email": "cruberryo@smugmug.com",
"ip_address": "222.10.201.47",
"latitude": 11.8554828,
"longitude": -86.2183907,
"city": "Dolores"
}
上面的JSON可以用下面的class表示:
public class UserEntity {
public Long id; //id is exact name field in JSON
@JsonProperty("first_name"); //other approach
public String firstName;
public String last_name;
public String email;
public String ip_address;
public Long latitude;
public Long longitude;
public String city;
}
现在,我们可以将 JSON 响应主体转换为 class,如下所示:
@Test
public void GetCity() {
Response response = when().
get(city).
then().
extract()
response();
UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}
“$”表示 JSON 文件的根目录(第一个对象 {})。这就是 Response 被翻译成 POJO 的方式。我们可以在非常相似的事情中做到这一点
Response response = when().
get(city).
then().
extract()
response();
UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
UserEntity userEntityFile = JsonPath.from(new File("file path"));
现在您可以像这样轻松地比较它们:
assertEquals(userEntityFile.id, userEntityResponse.id);
您也可以覆盖 hashCode()
和 equals()
方法,但如果您只是在学习,那可能太多了:)