我正在尝试将Json响应提取为以下代码中的字符串类型列表-
List<String> titleTypeAssertions = response.getBody().path("content.items.type");
在System.out.println(titleTypeAssertions);
之后
结果如下:很明显,该列表存储在变量titleTypeAssertions
[[image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, image, gallery, image, image, image, image, image, image, image, image, image, image, image, image, image, image, page, page, page, page, page, page, image, article, image, image, image, article, image, image, image, image, image, augmPhysStore, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, image, article, image, page, article, video, image, image, article, article, page, video, image, image, article, page, article, image, image, image, page, page, image, page, image, image, page, page, page, article, article, page, article, page, page, image, image, page, augmentedCategory, augmentedCategory, augmentedCategory, article, video, page, article, article, gallery, gallery, gallery, gallery, gallery, article, article, article, article, article, article, article, video, page, video, page, article, image, image, image, page, image, image, image, image, page, video, image, video, image, image, image, image, video, video, video, video, video, page, page, page, page, page, page, page, page, page, page, image, image, image, image, image, image, image, image, image, page, image, video, image, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, page, video, video, page, page, page, image, image, video, article, article, page, image, article, article, page, gallery, article, article, article, article, article, article, article, article, article, article, article, article, article, article, article, image, image, image, image, image, image, image, image, image, video, video, image, image, image, image, image, image, image, article, image, article, video, video, image, image, image, image, article, image, article, page, image, image, image, image, video, video, video, video, video, video, video, video, video, video, video, video]]
但是当我尝试这个断言时-
Assert.assertTrue(titleTypeAssertions.contains("image"));
它引发以下错误-
java.lang.AssertionError at com.salmon.test.step_definitions.api.coremedia.ComponentApiSteps.theApiShouldReturnsValidJsonResponseWithStatusAndContainTitleInAscendingOrder(ComponentApiSteps.java:127)
不确定此处的问题是什么。
这是一些json响应,显示为验证(response.getBody().path("content.items.type");
-
{
"contentKey": "sorting",
"version": "1.2",
"lang": "ru_RU",
"country": "ru",
"content": [
{
"type": "queryList",
"pageNumber": 1,
"items": [
{
"type": "image",
"thumbnailUrl": "http://coremedia.imagesphere.dev.product.ext.net-a-porter.com/testfolder2/6/w84.jpg",
"externalDamItemId": "imagesphere-ton:///dam/picture/testfolder2-6.jpg",
"title": "6.jpg",
"externalDamItemName": "6.jpg",
"mimeType": "image/jpg",
"imageUrl": "http://coremedia.imagesphere.dev.product.ext.net-a-porter.com/testfolder2/6/w500.jpg",
"focusPoint": {
"x": "0.5",
"y": "0.5"
},
"focusArea": {
"x1": "0.0",
"y1": "0.0",
"x2": "1.0",
"y2": "1.0"
},
"callToActionEnabled": true,
"locale": "ru-RU"
},
{
"type": "image",
"externalDamItemId": "throntestbw3:///dam/picture/c356c820-f70b-4099-877d-d645a545cd71",
"mimeType": "image/jpeg",
"imageUrl": "/ycm/resource/blob/121826/21D4C5DE5ECAB9B6C1A98FE8DD710347/hc-jpg-data.jpg",
"focusPoint": {
"x": "0.5",
"y": "0.5"
},
"focusArea": {
"x1": "0.0",
"y1": "0.0",
"x2": "1.0",
"y2": "1.0"
},
"callToActionEnabled": true,
"locale": "ru-RU"
},
{
任何帮助将不胜感激! 谢谢!
进一步,我更改了路径并尝试-
List<String> titleTypeAssertions = response.getBody().path("content.items.type[0]");
这些断言似乎有用-
assertThat(titleTypeAssertions.contains("image"),is(true));
assertThat(titleTypeAssertions.contains("gallery"),is(true));
assertThat(titleTypeAssertions.contains("article"),is(true));
assertThat(titleTypeAssertions.contains("augmentedCategory"),is(true);
assertThat(titleTypeAssertions.contains("video"),is(true));
assertThat(titleTypeAssertions.contains("augmPhysStore"),is(true));
但不是这个-
assertThat(titleTypeAssertions,containsInAnyOrder("image","gallery","article","video","augmPhysStore","augmentedCategory"));
它引发以下错误-
java.lang.AssertionError:
Expected: iterable over ["image", "gallery", "article", "video", "augmPhysStore", "augmentedCategory"] in any order
but: Not matched: "image"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)