Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将整个Json文件与之进行比较';s使用重新设置的响应?_Java_Json_Testng_Rest Assured - Fatal编程技术网

Java 如何将整个Json文件与之进行比较';s使用重新设置的响应?

Java 如何将整个Json文件与之进行比较';s使用重新设置的响应?,java,json,testng,rest-assured,Java,Json,Testng,Rest Assured,我用的是我几年前用过的重新发行的东西。这样做时,我的项目中有一个包含Json文件的文件夹。我将这些与API响应的实际结果进行比较。最好的办法是什么。我显然需要我的文件在我的项目中的位置,并需要将其与我的响应进行比较。有没有一个标准的方法来做到这一点 原来我有这个。我只是想从尸体上检查一下这个城市,但我想要整件事 @Test public void GetCity() { given(). when(). get(city).

我用的是我几年前用过的重新发行的东西。这样做时,我的项目中有一个包含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场景可以在一个数据表中测试多个响应。我的问题是如何实现上述目标

    {
  "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可以用下面的类表示:

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文件的根(第一个对象{})。这就是如何将响应转换为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()
方法,但如果您只是在学习:)

我从问题中了解到的是从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文件的根(第一个对象{})。这就是如何将响应转换为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的模式,而不是实际内容。将两者保存到
POJO
中,然后使用
equals()
method@Fenio我已经读了教程,我想我明白了。如果列表中有1000个用户呢。这肯定不是最好的办法?这也许正是POJO是个好主意的原因。您只需创建
List
,其中
UserEntity
是单个对象的表示。然后,您可以使用
for
循环或
列表
上进行迭代,以提高效率。否则你会怎么做?我个人有一个项目,我需要比较多个JSON,每个JSON有1800-7000万行,我使用StreamAPI的
parallelStream()
。@Fenio谢谢,因为我很新,我只是在练习,你能提供一个代码示例吗?我通常只是比较JSON的模式,不是实际内容。将两者保存到
POJO
并使用
equals()
method@Fenio我已经读了教程,我想我明白了。如果列表中有1000个用户呢。这肯定不是最好的办法?这也许正是POJO是个好主意的原因。您只需创建
List
,其中
UserEntity
是单个对象的表示。然后,您可以使用
for
循环或
列表
上进行迭代,以提高效率。否则你会怎么做?我个人有一个项目,我需要比较多个JSON,每个JSON有1800-7000万行,我使用StreamAPI的
parallelStream()
,用POJO做了比较。@Fenio谢谢,因为我是个新手,我只是在练习,您能提供一个代码示例吗?免责声明:我是从内存编写的,所以可能包含语法错误。免责声明:我是从内存编写的,所以可能包含语法错误。