java - 高枕无忧, float 负零

标签 java rest rest-assured

我想使用 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/

相关文章:

rest - 证书发现服务

java - 如何在放心地实现 get 请求时设置特定条件的等待

java - 对于未实现 AutoCloseable 的类,使用 lambda 是一种安全、正确且等效的解决方法吗?

java - 如何监控长时间运行作业的 REST 端点

Java - 无法通过 url 读取图像

rest - API设计: how to handle third party's APIs errors

java - 如何在 RestAssured 中将基本身份验证作为请求 header 的一部分传递?

java - 使用 Jayway 的放心验证 XML 属性

Java以这种格式减去两个日期

java - 锯齿状的 Java int 数组