Saturday, July 2, 2016

RESTful API Testing with Rest-Assured (1)

Before I start writing about API testing let me announce big news. I released all my Selenium related Java code snippets on Github Awesome Testing project. I will update it with every new technical post (including this one - the Rest-Assured tests were already pushed here). You can follow it and just type 'git pull' to get latest code updates. From my experience it's always better to check how things work with IDE/Maven support.

Some time ago in my Tester's toolbox - an alternative guide post I recommended you online resources on which you can train your testing skills. JSONplaceholder is perfect example of free RESTful API which can be utilised for improving API testing skills. In this post I'll show you how to write tests in Rest Assured framework.

1. Getting started

Rest-Assured tests follow Behaviour-Driven Development given, when, then approach. As Martin Fowler nicely explained it:
The essential idea is to break down writing a scenario (or test) into three sections:
  • The given part describes the state of the world before you begin the behavior you're specifying in this scenario. You can think of it as the pre-conditions to the test.
  • The when section is that behavior that you're specifying.
  • Finally the then section describes the changes you expect due to the specified behavior.
In order to use Rest-Assured you need to add the following libraries to your project.

2. Writing first test

Before I start let me explain convention that I use for Rest-Assured tests. Basically I split scenarios to two classes:

a) The first one, called NameAPI (for example JsonPlaceholderAPI) is responsible for defining requests (given + when) and returning ready to validate responses. Each method name in this class start with http method name (get, post, delete...).

b) The second class, called NameTest (for example GetTest) contains all the assertions (then). 

This approach nicely separates setup and assertion phase. You don't have to follow this path though, that's my way of keeping those tests readable and maintainable.

Here is how to code looks like

We are checking here that get request for gives us correct title. As you can see that's indeed the case.

Rest-Assured uses Hamcrest matchers for validation. There are quite a few of them (I used 4 different ones), and they are easily expandable. I will show you how to write custom matchers in one of the future posts soon.

3. Schema validation

Sometimes you may not want to assert details. You care more about the response types and structure. Rest-Assured supports this approach nicely with Schema Validation. SchemaValidationTest shows you how it works:

You just need to to define the schema:


4. Other methods

Obviously Rest-Assured isn't limited to get method only. You can add data as well. Let me show how you can simulate an user adding something on your forum. Jsonplaceholder requires UTF-8 encoding, so I needed to modify it.

Here is the test:

You may wonder if you have to write custom JSON parser. Fortunately Rest-Assured does all the work for us, and you just have to specify basic class:

Simple, isn't it? :)

All tests are available for download on my GitHub project.

No comments:

Post a Comment