【问题标题】:Why does Rest Assured say "validation doesn't match" but excepted does match Actual?为什么 Rest Assured 说“验证不匹配”,但例外确实匹配实际?
【发布时间】:2019-01-04 17:52:03
【问题描述】:

我正在尝试使用 RestAssured 验证此 JSON:

{
    "valueBounds": [
        {
            "bound": {
                "min": 1.0,
                "max": 4.2
            },
            "date": "2019-01-04T18:40:28.204+0100"
        }
    ],
}

使用此代码:

given().when().get(rootPath + "/test/").then().statusCode(200).log().body().
    body("valueBounds.bound.min", hasItems(1.0));

为什么我得到它们不匹配:

java.lang.AssertionError: 1 expectation failed.
JSON path valueBounds.bound.min doesn't match.
Expected: (a collection containing <1.0>)
Actual: [1.0]

我尝试过使用 Arrays.asList(1.0) 但这不是问题。

【问题讨论】:

  • 您正在使用 Hamcrest 匹配器 hasItems()。尝试使用 equalTo() 代替
  • 它非常明确地说:期望“一个集合”,但得到一个普通的数字。

标签: java rest-assured


【解决方案1】:

解决办法是在数字后面加一个f:

"valueBounds.bound.min", hasItems(1.0f)

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 2015-07-26
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2020-05-01
    • 2012-12-06
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多