【问题标题】:Rest Assured Body handling ArrayList放心的身体处理 ArrayList
【发布时间】:2016-10-07 05:40:52
【问题描述】:

我有这样的响应正文

enter code here

{
"applicationName": "Service MyService",
"someData": [
    {
        "name": "check1",
        "props": [
            "AAaa"
        ]
    },
    {
        "name": "check2",
        "props": [
            "BBbb",
            "CCcc"
        ]
    }
]
}

现在我可以使用下面的代码并且测试通过了。

        given().log().all()
        .accept(JSON).expect().statusCode(SC_OK)
    .when().log().all()
        .get(contextPath + "/test")
    .then().log().all()
        .body("someData.name",
            IsCollectionWithSize.hasSize(2))
        .body("someData.name",
            allOf(hasItems("check1", "check2")))
        .body("someData.findAll {it.name == 'check1'}.props", 
            IsCollectionWithSize.hasSize(1))
        .body("healthReports.findAll {it.name == 'check2'}.props", 
            IsCollectionWithSize.hasSize(2)));

但是,如果我随后尝试检查 props 字段中的值,我认为它会失败,因为返回了 ArrayList 并且匹配器正在检查 String。

        given().log().all()
        .accept(JSON).expect().statusCode(SC_OK)
    .when().log().all()
        .get(contextPath + "/test")
    .then().log().all()
        .body("someData.name",
            IsCollectionWithSize.hasSize(2))
        .body("healthReports.findAll {it.name == 'check1'}.props", 
                    IsCollectionContaining.hasItems(startsWith("AA")));

我不确定如何从 findAll ...props 中检查 ArrayList 的内容。

显示的错误是:

JSON path someData.findAll {it.name == 'check1'}.props doesn't match.
Expected: (a collection containing a string starting with "AA")
Actual: [[AAaa]]

有什么想法吗?

【问题讨论】:

  • 我应该给出错误

标签: rest-assured


【解决方案1】:

findall 返回一个 Array of Array,其中包含一个 AA 元素(这就是您使用 [[AAaa]] 而不是 [AAaa] 的原因。 您必须展平或提取一级数组才能解决我认为的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-12
    • 2021-12-07
    • 2021-12-10
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    相关资源
    最近更新 更多