Might be missing something obvious here so apologies if I'm being dumb!
I have a number of assertions I'd like to run against a response and I can see how simple this is to do by chaining with the RA DSL or building up a RequestSpecification with multiple assertions and finishing off with a get() or post().
To get this to work with the Cucumber way of running tests, I have tweaked the standard Given-When-Then pattern to Given-Then-When. So I have something like...
# my.scenario
Scenario:
Given I specify a value of 2 and action of double
Then the response code should be 200
And the result should be 4
When I submit to my Calculator API
# My.java
...
RequestSpecification requestSpec = RestAssured.with();
@Given("^I specify a value of (\\d+) and action of (double|halve)$")
public void I_specify_a_value_and_action(int value, String action) throws Throwable {
requestSpec.given().param("value", value);
requestSpec.given().param("action", action);
}
@Then("^the response code should be (\\d+)$")
public void the_response_code_should_be(int respCode) throws Throwable {
requestSpec.expect().statusCode(200);
}
@Then("^the result should be (\\d+)$")
public void the_result_should_be(int expectedResult) throws Throwable {
requestSpec.expect().body("calc.result", equalTo(expectedResult));
}
@When("^I submit to my Calculator API$")
public void submit_to_my_Calculator_API() throws Throwable {
}
...
As expected Cucumber runs through the scenario steps in the order defined in my.scenario and the all the assertions are performed in the final "When" step. So we've achieved a number of good things - separation of scenario and step definitions, reusable step definitions, using RestAssured to simplify the API calls and running assertions, etc.
However, as all the assertions are run in the "When" step, the existing Cucumber reporting and tooling will claim the "Then' steps passed successfully so it's not immediately obvious what has failed. The user is expected to dig in to the "When" step failure report and decipher all the assertion fails in one.
Ideally we'd like to see the relevant "Then" steps failing in the Cucumber reports where the contained RestAssured assertions fail. To do this I thought we might be able to revert back to the traditional Given-When-Then pattern and re-use the Response object generated in the When step in each of the Then steps. I want to avoid making the request multiple times in case it modified resource state.
In my head Maybe something like..
# my2.scenario
Scenario:
Given I specify a value of 2 and action of double
When I submit to my Calculator API
And the result should be 4
Then the response code should be 200
# My2.java
...
Response response;
@Given("^I specify a value of (\\d+) and action of (double|halve)$")
public void I_specify_a_value_and_action(int value, String action) throws Throwable {
requestSpec.given().param("value", value);
requestSpec.given().param("action", action);
}
@When("^I submit to my Calculator API$")
public void submit_to_my_Calculator_API() throws Throwable {
}
@Then("^the response code should be (\\d+)$")
public void the_response_code_should_be(int respCode) throws Throwable {
RequestSpecification requestSpec = RestAssured.with();
requestSpec.expect().statusCode(200).when().withResponse(response); // <-- Recycling the response (I've made up withResponse())
}
@Then("^the result should be (\\d+)$")
public void the_result_should_be(int expectedResult) throws Throwable {
RequestSpecification requestSpec = RestAssured.with();
requestSpec.expect().body("calc.result", equalTo(expectedResult)).when().withResponse(response); // <-- Recycling the response (I've made up withResponse())
}
...
This code assumed a lot; i.e. that the Response object is fully populated with the result of the GET.
Is there an existing or better way I could achieve my objective?
Thanks
J
--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.