当我尝试从json数组中匹配一个double值时,它失败了,并且期望值周围带有尖括号。这是什么意思?

时间:2019-09-05 13:34:33

标签: rest-assured-jsonpath

使用RestAssured,我尝试检查以下JSON:

    {
        "id": "abc1",
        "commonName": "Plane",
        "location": [
            1.1,
            1.1
        ]
    }

带有以下Java代码:

    double[] location = new double[]{1.1,1.1};
    given()
    .when()
        .get("tree/abc1/")
    .then()
        .assertThat()
        .statusCode(HttpStatus.OK.value())
        .body("location[0]", is((location[0])));

最后一个断言失败,并出现以下错误

    java.lang.AssertionError: 1 expectation failed.
    JSON path location[0] doesn't match.
    Expected: is <1.1>
      Actual: 1.1

期望值周围的尖括号表示什么?如何使断言成功?

2 个答案:

答案 0 :(得分:1)

返回的浮点数和翻倍为BigDecimal

  

而不是具有两种不同类型的配置,而是全部转换   浮动并翻倍为BigDecimal,然后在   hamcrest Matcher

enter image description here

答案 1 :(得分:0)

使用放心时,JSON编号的默认类型为float。我认为尖括号表示类型不匹配。

解决方案是在给定的块中设置放心的配置以指定数字类型。

double[] location = new double[]{1.1,1.1};
given()
    .config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(JsonPathConfig.NumberReturnType.DOUBLE)))
.when()
    .get("tree/abc1/")
.then()
    .assertThat()
    .statusCode(HttpStatus.OK.value())
    .body("location[0]", is(location[0]));