RESTful API Testing with Rest-Assured (1)
Before I start writing about API testing let me announce the big news. I released all my Selenium related Java code snippets on the 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 the 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 a perfect example of free RESTful API which can be utilized for improving API testing skills. In this post, I’ll show you how to write tests in the Rest Assured framework.
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.
To use Rest-Assured, you need to add the following libraries to your project.
Writing the first test
Before I start let me explain the convention that I use for Rest-Assured tests. Basically, I split scenarios into 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 starts with the HTTP method name (get, post, delete…).
b) The second class, called NameTest (for example GetTest) contains all the assertions (then).
This approach nicely separates the 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 a request for http://jsonplaceholder.typicode.com/posts/1 gives us the 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.
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 define the schema:
Obviously, Rest-Assured isn’t limited to get method only. You can add data as well. Let me show how you can simulate a user adding something to 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 a 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.
Tags: API testing, rest-assured, test automation
Categories: API testing