The land of Shattered dreams

Welcome to the online revolution…

Playing With Rest-Assured

| Comments

We are moving to a service oriented architecture at work based using technology from Apigee which is helping us build securable and scalable APIs for our applications to use.

Our APIs are based on RESTful Web Services with JSON message bodies. This is means that they can be used by anything that can send a HTTP request, including a browser.

One of the challenges is testing these APIs. Being a Java shop, sending the HTTP requests can be a bit of a faff, as can parsing the resultant JSON responses. We want the tests to be automated, and seeing as we use Junit and Hamcrest something that works in a similar way would be good. After a bit of googling, one of my colleagues pointed me in the direction of Rest-Assured.

Rest-Assured is a domain specific language for the easy testing of REST services, and attempts to bring the simplicity of using a dynamic language such a Ruby or Groovy to Java. It does this pretty well!

What I particularly like is that you can set up a fairly complex scenario with a minimum of code and it is really easy to see what is being tested. The documentation on the website is really good, but as an example:

Assume that a GET request to http://localhost:8080/ returns JSON as:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
    "lotto":{
         "lottoId":5,
         "winning-numbers":[2,45,34,23,7,5,3],
         "winners":[{
               "winnerId":23,
               "numbers":[2,45,34,23,3,5]
         },{
               "winnerId":54,
               "numbers":[52,3,12,11,18,22]
         }]
    }
}

Then:

1
expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");

Verifies that the lottoId is 5 when you get it. It’s easy to read, simple to setup, and really powerful…

Comments