使用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
期望值周围的尖括号表示什么?如何使断言成功?
答案 0 :(得分:1)
答案 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]));