【发布时间】:2026-02-06 18:05:02
【问题描述】:
我正在使用 RestAssured 和 Hamcrest 编写一些集成测试。 在我尝试检查嵌套属性之前一切都很好。
我的 Json 身体是:
{
"rows": [
{
"uid": "927e2362-babb-47cc-8406-d618b0e15b89",
"owner": "myself"
},
{
"uid": "6d39c473-d0bd-496e-be86-40917aa3af79",
"owner": "myself"
}
]
}
我的测试代码是这样的:
ValidatableResponse response = request.when().get(path).then();
response.statusCode(HttpStatus.SC_OK);
现在我正在尝试添加一个检查,确保所有行都具有属性 owner,其值为 myself
我能想到的最好的方法是:
response.body("rows.owner", everyItem(is("myself")));
但是我更喜欢使用匹配器 Matchers.hasProperty("owner") 甚至 HasPropertyWithValue.hasProperty("owner", is("myself")
我尝试了几种方法都没有运气:
List<Object> o = response.extract().jsonPath().get("rows");
assertThat(o, hasItem(hasProperty("owner")));
assertThat(o.get(0),Matchers.hasProperty(TestConstants.DOC_OWNER));
但我每次都得到
预期:包含 hasProperty("owner") 的集合 但是: 中没有“所有者”,没有“所有者”
我看过这个subjet,它解释说我最终可能会得到一个数组数组。但我看不出如何以最简单的方式做到这一点: - 检查我列表中的每个项目是否都有属性 "owner" 并且其值始终为 "myself"(用于请求的过滤器)
【问题讨论】:
标签: rest integration-testing rest-assured hamcrest