问题描述
放心:我们如何使用 Hemcrest 匹配器而不是使用 Foreach 循环将 Json 数组中的每个元素与 Java 中的一个特定相同值进行比较。
{
"id": 52352,"name": "Great Apartments","floorplans": [
{
"id": 5342622,"name": "THE STUDIO","fpCustomAmenities": [
{
"displaySequence": 2,"amenityPartnerId": "gadasd","display": true,"leased": true
},{
"displaySequence": 13,"amenityPartnerId": "sdfsfd","leased": true
}
]
},{
"id": 4321020,"name": "THE First Bed","fpCustomAmenities": [
{
"displaySequence": 4,{
"displaySequence": 15,"amenityPartnerId": "hsfdsdf","leased": true
}
]
}
]
}
我想比较 json 响应中所有级别的所有租用节点的 Leased=true...
我有工作代码...
List<List<Boolean>> displayedvaluesfpStandardAmenities =
when().get(baseUrl + restUrl).
then().statusCode(200).log().ifError().
extract().body().jsonPath().getList("floorplans.fpCustomAmenities.display");
for (List<Boolean> displayedStandardList : displayedvaluesfpStandardAmenities) {
for (Boolean isDisplayedTrue : displayedStandardList) {
softAssert.assertTrue(isDisplayedTrue);
}
}
但问题是我需要使用 Hemcrest Matchers 或 Restaussred Matchers 的简单格式的代码,并尝试像下面这样的简单方法,(这是行不通的)
when().get(baseUrl + restUrl).
then().assertThat().body("floorplans.fpCustomAmenities.display",equalTo("true"));
我得到的错误是
java.lang.AssertionError: 1 expectation failed.
JSON path floorplans.fpCustomAmenities.display doesn't match.
Expected: true
Actual: <[[true,true],[true,true]]>
所以我需要的是 json 响应中所有需要与“true”进行比较的“显示”节点,以便我的测试可以通过。
我有上面提到的替代解决方案,但我需要的只是使用匹配器的工作解决方案。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)