Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare\assert double values in rest assured

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

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))

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

p.s. it works if

body("value", Matchers.equalTo(12f))
like image 205
Dmitriy Zholudev Avatar asked May 13 '19 14:05

Dmitriy Zholudev


2 Answers

RestAssured parses numbers as Float by default.

Based on github issue 1315 You can configure it to parse it to Double:

JsonConfig jsonConfig = JsonConfig.jsonConfig()
    .numberReturnType(JsonPathConfig.NumberReturnType.DOUBLE);

RestAssured.config = RestAssured.config()
    .jsonConfig(jsonConfig);
like image 180
Nikolai Golub Avatar answered Sep 20 '22 07:09

Nikolai Golub


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)))
like image 26
Sumit Bhardwaj Avatar answered Sep 22 '22 07:09

Sumit Bhardwaj