如何放心地比较\断言双值 [英] How to compare\assert double values in rest assured

查看:36
本文介绍了如何放心地比较\断言双值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JSON 中的\assert double 与 java 原始 double 值进行比较.正确的做法是什么?

I'm trying to compare\assert double from a JSON with java primitive double value. What is the proper way to do it?

我使用了简单而常规的方法,使用 Matchers.equalTo 方法,见下文

I used simple and regular way to do it, using Matchers.equalTo method, see below

public class A{
       private static double someJavaDouble = 12
}

given().
        header(.....).
when().
        get(url).
then().
        statusCode(200)
        body("value", Matchers.equalTo(someJavaDouble))

get(url) 的响应是 JSON:

Response of get(url) is JSON:

{
    "success": true,
    "currentValue": 12.0
}

在上面的代码中我得到这个错误:

In the code above I get this error:

JSON path currentValue doesn't match.
Expected: <12.0>
  Actual: 12.0

附言如果

body("value", Matchers.equalTo(12f))

推荐答案

由于JSON Serializer返回的值是Double,而不是原始数据类型double.
您可以从 Double Double.doubleValue() 获取 double 值或将 double 转换为 Double new Double(someJavaDouble)

Since the value returned by JSON Serializer is Double, not the primitive data type double.
You can get double value from Double Double.doubleValue() or convert double to Double new Double(someJavaDouble)

body("value", Matchers.equalTo(new Double(someJavaDouble)))

这篇关于如何放心地比较\断言双值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆