hasKey 没有看到响应中的字段

hasKey doesn't see the field in the response

我有测试:

   @Test
   public void sixTest() {
       FlowerList flowerList = new FlowerList();
       Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
       Response response = given()
                .when()
                .get("/api/unknown")
                .then()
                .log().all()
                .body("data.year", hasKey("2001"))
                .extract().response().as((Type) FlowerList.class);

我需要检查至少有一个年份字段包含值 2001。但我得到一个例外:

Expected: map containing ["2001,"->ANYTHING]
  Actual: [2000, 2001, 2002, 2003, 2004, 2005]

我做错了什么?理论上 hasKey 应该 return 单个值 2001

得到:

{
    page: 1,
    per_page: 6,
    total: 12,
    total_pages: 2,
    data: [
    {
    id: 1,
    name: "cerulean",
    year: 2000,
    color: "#98B2D1",
    pantone_value: "15-4020"
    },
    {
    id: 2,
    name: "fuchsia rose",
    year: 2001,
    color: "#C74375",
    pantone_value: "17-2031"
    },
    ... 

错误信息给你答案

Expected: map containing ["2001,"->ANYTHING]
  Actual: [2000, 2001, 2002, 2003, 2004, 2005]

实际结果是一个数组,而不是一个映射。因此
Matchers.hasItems(如果 Iterable 有项目则匹配)
应该使用而不是
Matchers.hasKeys(如果 Map 有键则匹配)

另一个问题是,由于year是整数,所以2001应该是预期值而不是"2001"

所以断言应该是

.body("data.year", hasItems(2001))