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))
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);
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With