Get JSON response from a REST API in Cucumber test
-
I'm delving into the world of test automation this week, it seems. Tasked, at this moment, with refactoring a set of Cucumber tests, written in Java. (And I didn't write them.)
I have to make requests to out REST API, and parse the response.
Currently the tests are using RestAssured to make the request and parse the response. This has certain idiosyncracies, notably that if you have any floating point values in the response will end up as floats and not doubles or BigDecimals or even Strings.
Given a response like:
{"foo":[ {"bar":1.0, "baz":"supercallifragilisticexpiallidocious" }, {"bar":1.3, "baz":"Hello World!" }], "quux": 2 }
then code to verify the value of
bar
would currently be like:response.then().body("foo[0].bar", equalTo(expectedValueOfBar) );
where
expectedValueOfBar
is a float. Currently I'm in the process of switching these fromFloat.parseFloat(someStringFromADataTable);
to
new BigDecimal(someStringFromADataTable).floatValue();
which seems marginally better to me.
Direct quote from
$leadDev
:There'd better not be any floats when I come to finally code review it.
Can anyone suggest
a) an alternative to RestAssured, or
b) any better way to use RestAssured, or
c) should I just point$leadDev
to the documentation that says floats are what you get?
-
@carrievs I'd tend to try C first.
-
Edit... nevermind, I did a brainfart.
-
Option C seems the best option to me. If software gives you floats, try not to drown.
If $leadDev disagrees that's fine, but then he should come up with a better approach. He is the lead dev after all and thus, should lead.
-
@alexmedia said in Get JSON response from a REST API in Cucumber test:
He is the lead dev after all and thus, should lead
The problem with some leaders is, sometimes, they lead you directly off a cliff
-
@timebandit So don't follow too closely behind?
-
@pleegwat said in Get JSON response from a REST API in Cucumber test:
@timebandit So don't blindly follow
too closely behindthem?FTFY
-
But... JavaScript numbers are all doubles. Why would they make them single-precision floats?
-
Option C successfully employed.