英文:
How do you compare an entire Json file to it's response using restassured?
问题
以下是翻译好的内容:
我正在使用之前几年使用过的 restassured。在这样做时,我的项目中有一个包含 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"
}
英文:
I'm using restassured something I used a few years back. When doing so I had a folder with Json files in my project. I was comparing these to the actual outcome of the API response. What is the best way to go about this. I obviously need my file location in my project and need to compare it to my response. Is there a standard way to do this.
So originally I had this. Just to check the City from the body but I want the whole thing.
    @Test
public void GetCity() {
    given().
            when().
            get(city).
            then().
            assertThat().
            body(("city"), equalTo(city));
}
But I want to get to something like this below:
@Test
public void GetCity() {
    given().
            when().
            get(city).
            then().
            assertThat().
            JsonFile(("/Myjson"), equalTo(response));
}
I'm using TestNg currently but I remembered using Cucumber Scenarios which allowed me to test multiple responses in a data table. Question I have is how do I achieve the above?
    {
  "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"
}
答案1
得分: 1
@Test
public void GetCity() {
    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);
}
First, we extract the Response object which contains information like status code or response body. In this case it will be JSON. Before we extract it, let's create a POJO with JSON representation:
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;
}
Now, we can transform the JSON response body into this class like this:
@Test
public void GetCity() {
    Response response = when().
        get(city).
    then().
        extract().
        response();
    UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}
The "$" means root of JSON file (the first object {}). This is how Response is translated into POJO. We can do it in a very similar matter:
Response response = when().
    get(city).
then().
    extract().
    response();
UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
UserEntity userEntityFile = JsonPath.from(new File("file path"));
Now you can easily compare them like:
assertEquals(userEntityFile.id, userEntityResponse.id);
You could also override hashCode() and equals() methods but that might be too much if you're just learning ![]()
英文:
What I understood from the question is to get response from an API and compare with JSON file. How do it:
 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();
}
First, we extract the Response object which contains information like status code or response body. In this case it will be JSON. Before we extract it, let's create a POJO with JSON representation:
{
  "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"
}
The above JSON can be represented by below 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;
} 
Now, we can transform the JSON response body into this class like this:
 @Test
public void GetCity() {
        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
}
The "$" means root of JSON file (the first object {}). This is how Response is translated into POJO. We can do it in a very similar matter
        Response response = when().
            get(city).
        then().
            extract()
            response();
        UserEntity userEntityResponse = response.jsonPath().getObject("$", UserEntity.class);
        UserEntity userEntityFile = JsonPath.from(new File("file path"));
Now you can easily compare them like:
assertEquals(userEntityFile.id, userEntityResponse.id);
You could also override hashCode() and equals() methods but that might be too much if you're just learning ![]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论