【问题标题】:RestAssured: How to check the length of json array response?RestAssured:如何检查 json 数组响应的长度?
【发布时间】:2016-07-27 22:53:10
【问题描述】:

我有一个返回 JSON 的端点:

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

还有一个 DTO 类:

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

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

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

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

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

【问题讨论】:

    标签: json rest-assured


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      喜欢:

      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));
      

      【讨论】:

        【解决方案3】:

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

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

        使用放心的 3.0.0

        【讨论】:

          【解决方案4】:

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

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

          现在我可以将响应正文提取为字符串并使用内置的 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);
          

          完整示例见http://olyv-qa.blogspot.com/2017/07/restassured-short-example.html

          【讨论】:

            【解决方案5】:

            @Héctor

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

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

            如果你创建这样的东西:

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

            现在你仍然通过了测试:

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

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

            【讨论】:

              【解决方案6】:

              这是你需要做的才能获得大小。

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

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

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

              希望这会有所帮助。 :)

              【讨论】:

                【解决方案7】:

                也可能的选项是hasSize() 匹配器以及"." 路径:

                import static org.hamcrest.Matchers.hasSize;
                
                ...
                
                @Test
                public void test() {
                    RestAssured.get("/foos").then().assertThat()
                      .body(".", hasSize(2));
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-10-29
                  • 2016-10-16
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多