当我尝试匹配 json 数组中的双精度值时,它失败了,预期值周围有尖括号。这是什么意思?
When I attempt to match a double value from a json array it fails with the expected value having angle brackets around it. What does this mean?
使用 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
预期值周围的尖括号表示什么,我怎样才能使断言成功?
JSON个数字的默认类型是float,放心使用。我认为尖括号表示类型不匹配。
解决方法是在给定的block中设置放心配置,指定号码类型
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]));
返回浮点数和双精度数作为 BigDecimal
instead of having two different type of configuration, Convert all
floats and doubles into BigDecimal and then Compare then in the
hamcrest Matcher
使用 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
预期值周围的尖括号表示什么,我怎样才能使断言成功?
JSON个数字的默认类型是float,放心使用。我认为尖括号表示类型不匹配。
解决方法是在给定的block中设置放心配置,指定号码类型
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]));
返回浮点数和双精度数作为 BigDecimal
instead of having two different type of configuration, Convert all floats and doubles into BigDecimal and then Compare then in the hamcrest Matcher