I am studying Rest Assured framework now.
Using some API I get the following (partial) JSON response:
{
"results": [
{
"type": "AAAA"
},
{
"type": "A"
}
]
}
I am trying to verify the types. The only way I found so far is to use gson to translate the string to an object and then assert:
@Given("^test2$")
public void test2$() {
RestAssured.baseURI = BASE_URI;
String response =
given()
.param(KEY_KEY, API_KEY)
.param(URL_KEY, URL_TO_CHECK)
.when()
.get(RESOURCE)
.asString();
System.out.println(response);
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
WhtResponse whtResponse = gson.fromJson(response, WhtResponse.class);
assertTrue(whtResponse.getResults().size() == 2);
assertTrue(whtResponse.getResults().get(0).getType().equals("AAAA"));
assertTrue(whtResponse.getResults().get(1).getType().equals("A"));
}
Please ignore that there are several asserts in one test method. I know it is not best practice but I am just "playing" now to study the material.
Is there a better, shorter, more fluent way to test both values? Maybe directly with Rest Assured and without the Gson?
Thanks!