RestAssured:如何检查 json 数组响应的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36469722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:22:52  来源:igfitidea点击:

RestAssured: How to check the length of json array response?

jsonrest-assured

提问by Héctor

I have an endpoint that returns a JSON like:

我有一个返回 JSON 的端点,如:

[
  {"id" : 4, "name" : "Name4"},
  {"id" : 5, "name" : "Name5"}
]

and a DTO class:

和一个 DTO 类:

public class FooDto {
    public int id;
    public String name;
}

Now, I'm testing the length of the returned json array this way:

现在,我以这种方式测试返回的 json 数组的长度:

@Test
public void test() {
    FooDto[] foos = RestAssured.get("/foos").as(FooDto[].class);
    assertThat(foos.length, is(2));
}

But, is there any way to do it without cast to FooDto array? Something like this:

但是,有没有什么办法可以在不强制转换为 FooDto 数组的情况下做到这一点?像这样的东西:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .length(2);
}

回答by Héctor

Solved! I have solved it this way:

解决了!我是这样解决的:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .body("size()", is(2));
}

回答by arulraj.net

There are ways. I solved with the below

有办法。我用下面的方法解决了

@Test
public void test() {
 ValidatableResponse response = given().when().get("/foos").then();
 response.statusCode(200);
 assertThat(response.extract().jsonPath().getList("$").size(), equalTo(2));
}

using restassured 3.0.0

使用放心 3.0.0

回答by olyv

I solved similar task wit GPath.

我用 GPath 解决了类似的任务。

Response response = requestSpec
                .when()
                .get("/new_lesson")
                .then()
                .spec(responseSpec).extract().response();

Now I can extract response body as String and use built-in GPath capabilities

现在我可以将响应正文提取为字符串并使用内置的 GPath 功能

String responseBodyString = response.getBody().asString();

assertThat(from(responseBodyString).getList("$").size()).isEqualTo(YOUR_EXPECTED_SIZE_VALUE);
assertThat(from(responseBodyString).getList("findAll { it.name == 'Name4' }").size()).isEqualTo(YOUR_EXPECTED_SUB_SIZE_VALUE);

For full example see http://olyv-qa.blogspot.com/2017/07/restassured-short-example.html

有关完整示例,请参阅http://olyv-qa.blogspot.com/2017/07/restassured-short-example.html

回答by Liniel

@Héctor

@赫克托

[   
   {"id" : 4, "name" : "Name4"}, 
   {"id" : 5, "name" : "Name5"}
]

It was really unlucky example. Here you have matrix [2,2].

这真是个倒霉的例子。这里有矩阵 [2,2]。

If you create something like this:

如果你创建这样的东西:

[   
   {"id" : 4, "name" : "Name4"}, 
   {"id" : 5, "name" : "Name5"}, 
   {"id" : 6, "name" : "Name6"} 
]

Now you still get passed test:

现在你仍然通过了测试:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .body("size()", is(2));
}

Consider about whether it was intentional. body("size()",is(2));Have checking length of one node instead of number of records in response.

考虑是否是故意的。 body("size()",is(2));检查一个节点的长度而不是响应的记录数。

回答by Michel

You can simply call size()in your body path:

你可以简单地调用size()你的身体路径:

Like:

喜欢:

given()
            .accept(ContentType.JSON)
            .contentType(ContentType.JSON)
            .auth().principal(createPrincipal())
            .when()
            .get("/api/foo")
            .then()
            .statusCode(OK.value())
            .body("bar.size()", is(10))
            .body("dar.size()", is(10));

回答by roy

This is what you need to do in order to get size.

这是您需要做的才能获得尺寸。

Response response = 
given()
.header("Accept","application/json")
.when()
.get("/api/nameofresource")
.then()
.extract()
.response();

once you have the response, you can do the following to get the size.

获得响应后,您可以执行以下操作来获取大小。

int size = response.jsonPath().getList("id").size();

int size = response.jsonPath().getList("id").size();

hope this helps. :)

希望这可以帮助。:)