【问题标题】:How do you compare an entire Json file to it's response using restassured?您如何使用 reassured 将整个 Json 文件与其响应进行比较?
【发布时间】:2020-12-18 08:30:14
【问题描述】:

我正在使用几年前使用的放心的东西。这样做时,我的项目中有一个包含 Json 文件的文件夹。我将这些与 API 响应的实际结果进行了比较。解决此问题的最佳方法是什么。我显然需要我的项目中的文件位置,并且需要将它与我的响应进行比较。有没有标准的方法来做到这一点。

原来我有这个。只是为了从身体上检查城市,但我想要整个东西。

    @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,它允许我在数据表中测试多个响应。我的问题是如何实现上述目标?

    {
  "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 的架构,而不是实际内容。
  • 将两者保存到POJO 并使用equals() 方法
  • @Fenio 我已经阅读了教程,我想我明白了。如果列表中有 1000 个用户怎么办。这肯定不是最好的做法吗?
  • 这可能正是 POJO 是个好主意的原因。您只需创建List<UserEntity>,其中UserEntity 是单个对象的表示。然后,您可以使用 for 循环或 Streams 迭代 List 以使其更加高效。否则你会怎么做?我个人有一个项目,我需要比较多个 JSON,每个 JSON 有 18-7000 万行,我使用 POJO 使用 StreamAPI 的parallelStream() 进行了比较。
  • @Fenio 谢谢,因为我是新手,我只是在练习,你能提供一个代码示例吗?

标签: java json testng rest-assured


【解决方案1】:

我从问题中了解到的是从 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 可以用下面的类来表示:

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 响应体转换为此类:

 @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() 方法,但如果您只是在学习,这可能太多了 :)

【讨论】:

  • 免责声明:我是凭记忆写的,因此可能包含语法错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多