我想用rest保证测试Rest服务,但是当服务返回负零值时,我的测试失败了。
放心测试:
String methodName="multiply";
float[] operands = {1f,-2.5f,0};
float result = operands[0] * operands[1] * operands[2];
Response response = given().
pathParam("a",operands[0]).
pathParam("b",operands[1]).
pathParam("c",operands[2]).
contentType(JSON).
log().ifValidationFails().
when().
get("/"+methodName+"/{a}/{b}/{c}").
then().
assertThat().statusCode(200).
body("result",equalTo(result));错误:
java.lang.AssertionError: JSON path result doesn't match.
Expected: <-0.0F>
Actual: 0.0结果json:
{"result":-0.0}当rest服务返回负零值时,为什么测试失败?
发布于 2016-11-26 19:40:31
我已经知道,最好是配置,请放心,将所有Json数字返回为BigDecimal,然后使用hamcrest closeTo matcher。
String methodName="multiply";
double[] operands = {1,-2.5,0};
double result = operands[0] * operands[1] * operands[2];
Response response = given().
config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL))).
pathParam("a",operands[0]).
pathParam("b",operands[1]).
pathParam("c",operands[2]).
contentType(JSON).
log().ifValidationFails().
when().
get("/"+methodName+"/{a}/{b}/{c}").
then().
assertThat().statusCode(200).
log().ifValidationFails().
body("result",closeTo(BigDecimal.valueOf(result),new BigDecimal("1E-20")));发布于 2019-05-05 11:46:16
根据最新的放心规则,我们将获得舍入值directly.However,如果您想验证精确的JSON休息(在Banking中),而不使用舍入逻辑,那么我们必须使用以下代码:-
BigDecimal xyz=JsonPath.with(response.asString()).using(new JosnPathConfig.NumberReturnType.BigDecimal)).get("JSON PATH")这里,response=JSON响应和JSON路径只不过是希望通过响应验证的确切的json字段。
https://stackoverflow.com/questions/40584072
复制相似问题