如何断言 body 中的 属性 只有几个预定义值

How to assert that a property in body has only a few predefined values

Json body:

{
  "result": [
    {
      "object": {
        "type": "mattress"
      }
    },
    {
      "object": {
        "type": "pillow"
      }
    }
  ]
}

如何使用 rest-assured body 和 hamcrest 断言来断言类型只是枕头或床垫(可以有更多,所以我正在寻找通用解决方案)?

示例断言:

response.then().assertThat().body("result", hasSize(greaterThan(0)));

此代码可以解决您的问题:

.body("result.object.type",  everyItem(isOneOf("mattress", "pillow")));