7

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))
  • Possible duplicate of [Confusion over REST Assured floating-point comparisons](https://stackoverflow.com/questions/46815071/confusion-over-rest-assured-floating-point-comparisons) – CryptoFool May 13 '19 at 15:10

2 Answers2

4

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);
Nikolai Golub
  • 3,327
  • 4
  • 31
  • 61
  • 1
    If you are wondering where you need to put this, you can put this inside @BeforeAll of your Test class. – Janardhan Nov 04 '22 at 11:35
1

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