Serenity BDD can help you create tests for REST services, with all the advantages that Serenity BDD introduces to Web Tests and more.

It provides tight integration with Rest Assured, along with some improvements and advanced reporting. All you need to do is import SerenityRest, and that’s all!

Reports created when Rest is tested

If you use SerenityRest in your tests then all your requests/responses will be included in the generated report, so you can easily explore the cookies, headers, response body, url as well as validate them in the RestAssured way. Here is example of generated report for some of demo tests:

request headers body
Figure 1. Request with Headers and Body Example
response headers body cookies
Figure 2. Response with Headers and Body and Cookies Example

As you see, all the requests are included in the report under the corresponding steps:

rest query in report
Figure 3. Rest Query included in report under steps

Writing tests with Rest Assured

All Rest Assured tests are valid tests for Serenity BDD. You can use all contractions like given-when-then or expect-when-then, initialise some query parameters, and so on.

...
    @Step
    public void whenIAddThePetToTheStore() {
        for (Pet pet : pets) {
            int id = Math.abs(new Random().nextInt());
            Map<String, Object> jsonAsMap = new HashMap<>();
            jsonAsMap.put("id", id);
            jsonAsMap.put("name", pet.getName());
            jsonAsMap.put("status", pet.getStatus());
            jsonAsMap.put("photoUrls", new ArrayList<>(Arrays.asList()));

            given().contentType("application/json")
                    .content(jsonAsMap).log().body()
                    .baseUri("http://petstore.swagger.io")
                    .basePath("v2/pet")
            .when().post();

            pet.setId(id);

        }
    }

or

...
    @Step
    public void thenPetShouldBeAvailable() {
        for (Pet pet : pets) {
            expect().that().statusCode(200)
                    .and().body("name", equalTo(pet.getName())).when()
                    .get("http://petstore.swagger.io/v2/pet/{id}", pet.getId());
        }
    }

You can create Steps classes in the normal Serenity BDD way and use them as reusable components. You’ll have access to use all the features of Serenity including the aggregated report. Also Serenity BDD introduces the possibility to share the same RestResponse between steps to allow writing Step methods in a more native way:

 ...
        @Step
        def getById(final String url) {
            rest().get("$url/{id}", 1000);
        }

        @Step
        def thenCheckOutcome() {
            then().body("Id", Matchers.anything())
        }

For easy configuration and resetting rest configuration you can use RestConfigurationRule. All Configuration action described in rule will executed before test and after test will be executed reset.

...
    @Rule
    def RestConfigurationRule rule = new RestConfigurationRule(new RestConfigurationAction() {
        @Override
        void apply() {
            SerenityRest.setDefaultBasePath(value)
        }
    })

Using split classes to initialise and configure Rest Assured

With Serenity BDD you can configure Rest Assured using chain of calls, that can be much more easy to read than a lot of separated lines:

...
new RestDefaultsChained().setDefaultBasePath("some/path")
    .setDefaultProxy(object).setDefaultPort(10)

Basically all what you can execute during rest tests included in one class 'SerenityRest', but you also use slitted classes to separate your logic and help yourself to find function what you need with 'RestUtility' and 'RestDefaults' and 'RestRequests'.