REST Assured 2.0 – Testing your REST services is easier than ever

REST Assured is a simple Java library for testing of REST services and a new major version has just been released. Not only is it easy to use and get started with but it’s also built to scale to more advanced use cases using detailed configuration, filters, specifications and the like. This version is simpler than ever mainly thanks to an updated syntax, given-when-then.

Old Syntax

Imagine that a REST service located at http://localhost:8080/lotto returns the following JSON document in its body:

The code below shows how to execute a GET request and validate that it’s ok (status code is 200), assert that the JSON document located in the response body has lotto id equal to 5 and that winner id’s are 23 and 54 in version 1.9.0 and earlier:

While this is quite simple it felt a bit awkward to many users to specify the assertions (expect) before the request has been sent. This is why REST Assured now supports an updated syntax of given-when-then.

New Syntax

With version 2.0 you now do like this instead:

Which to most users feels more natural and familiar.

Extracting values

It’s also very easy to make requests and extract values out of the response with REST Assured. This is an example of how to return the winning-numbers list from the lotto JSON document:

But what if you want to make some assertions and then return the winning numbers? This is also very easy and looks like this:

That’s it!

Conclusion

If you consider upgrading from an earlier version of REST Assured you can rest assured (!) that the old syntax will still work just as before. It’s just that the given-when-then syntax is as of 2.0 the recommended way of writing your tests. There are some benefits of using the older syntax though. The most prominent one is that REST Assured will present ALL failing assertions at the same time, something that is not possible with the updated syntax. If you’re interested in reading more about the new release refer to the release notes and usage guide.

1 Comment

  1. Dean Hayat

    Hello Johan,

    Just wanna ask something. I want to automate the REST services at work. Since I am new to REST assured and even to automation I am keen to know is there any step-by-step explanations to start with it. I couldn’t find any useful info online.

    I have tried with eclipse. Created a java project -> a package -> a class. Also included all the JAR files and here is my code. It’s failing and can you please help me? Thank you.

    package test.rest.services;
    import static com.jayway.restassured.RestAssured.*;
    import org.junit.Test;

    public class restSearchServices
    {
    @Test
    public void searchServices()
    {
    expect().
    statusCode(200).
    when().
    get(“https://rest..(MY REST URL)./searchservice.svc/search”);
    }
    }

Leave a Reply