我想使用 Rest Assured 测试我的休息服务,但当服务返回负零值时,我的测试失败。
放心测试:
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 Assured 以将所有 Json 数字作为 BigDecimal 返回,然后使用 hamcrest closeTo 匹配器。
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")));
关于java - 高枕无忧, float 负零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584072/