Skip to main content

REST-Assured framework overview

In modern software development, REST services becomes most popular choice for implementing distributed and scalable web application. They are light and easy to maintain, which results in faster and more effective implementation and system integration.

I recommend you also my other posts about REST-Assured and building microservice’s test automation frameworks:
  • REST-Assured: going deeper
  • Building microservices testing framework

With the increase popularity of RESTful services, there is a need for fast and lightweight tool for REST webservices testing automation. One of the most popular choice is Rest-Assured framework from Jayway. It introduces simplicity of testing web services from dynamic languages like groovy or ruby to java. In this post we will get our hands dirty and write automatic test in Rest-Assured framework.

In order to create complete implementation of automated tests in Rest-Assured framework, we need to write our code against some example API. We’ll use standalone Wiremock mock server, which we had created in previous article (full API documentation with setup tutorial can be found here) or you can just clone working project from my github. Endpoints specifications of API that we will test look as follows:

First method:

  • request:


  • response:


Second method:

  • request:


  • response:


Setting up 

First thing which we have to do is of course to add Rest-Assured dependencies to our project. If you are using gradle, add this dependency to your TestCompile scope:

testCompile 'io.rest-assured:rest-assured:3.0.6'

and the same for Maven users:

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.0.6</version>
    <scope>test</scope>
</dependency>

You can also add libraries to your classpath manually. After refreshing or rebuilding our project we are able to use Rest-Assured. The last thing is to use static import in our test class:

import static io.restassured.RestAssured.*;

Writing tests 

Rest-Assured is using given/when/then notation. In Given section we declare things like content type or request body. In When section we provide HTTP method and endpoint. In Then section we declare response verification. The method hitting our first API method looks as follows:


Every section in method body corresponds to our mock declaration. We are making GET request on /user/{userID} endpoint, and then we check if response contains id, login and www elements, and if the response status is 200. After declaring getUserData method, we can write test method (RestService is a class that contains getUserData method):


Second endpoint in our mock provide POST method. Request body should be declared in Given section, and the rest looks similarly:


…and the test methods that calls method above:


Extras 

Although that main test flow in Rest-Assured is pretty straightforward, framework equips us with many useful features. One of my favourite is log method. Below is an example of using built-in logger in rest-assured method. No magic here, just call log().all() method:


Another important feature is HTTPS Validation. If you have some HTTPS issues, and your test flow doesn’t check any security cases, you can simply call:

RestAssured.useRelaxedHTTPSValidation();

One of the most obvious use case would be extracting values. With Rest-Assured 2.0 it couldn’t be easier. Here’s an example of extracting login value from getUserData() method response:


Those are just few examples. Rest-Assured exposes rich API, that makes your test development easy and effective.

Continue reading 

If you want to continue reading and expand your knowledge in area of REST and microservices, I recommend you these books:
  • Building Microservices – one of the most important books for me, everything you want to know about microservices is here
  • Java For Testers: Learn Java fundamentals fast – test automation does not require complex programming knowledge. Learn fundamentals of Java for test automation. From tester to testers!
  • RESTful Web APIs – another great book about REST architecture. Lots of practical knowledge about designing and consuming RESTful APIs


Summary 

When it comes to test RESTful architecture in Java, Rest-Assured should be your first-choice framework. Test development is easy and efficient. You can learn more about framework in official documentation or go through excellent examples in official jayway github. Complete project from this post is avabile for download from here.

Popular posts from this blog

Testing Asynchronous APIs: Awaitility tutorial

Despite the growing popularity of test automation, most of it is still likely to be done on the frontend side of application. While GUI is a single layer that puts all the pieces together, focusing your automation efforts on the backend side requires dealing with distributed calls, concurrency, handling their diversity and integration. Backend test automation is especially popular in the microservices architecture, with testing REST API’s. I’ve noticed that dealing with asynchronous events is particularly considered as challenging. In this article I want to cover basic usage of Awaitility – simple java library for testing asynchronous events. All the code examples are written in groovy and our REST client is Rest-Assured. Synchronous vs Asynchronous  In simple words, synchronous communication is when the API calls are dependent and their order matters, while asynchronous communication is when the API calls are independent. Quoting Apigee definition: Synchronous  If a

Rerun Flaky Tests – Spock Retry

One question I get asked a lot is how you can automatically rerun your test on failure. This is a typical case for heavy, functional test scenarios, which are often flaky. While test flakiness and its management is crucial and extensive matter itself, in this post I want to give a shout to the extremely simple yet useful library: Spock-Retry. It introduce possibility to create retry policies for Spock tests, without any additional custom-rules implementation – just one annotation. If you are not a fan of Spock testing framework and you prefer JUnit – stay tuned! I will post analogous bit about rerunning JUnit tests soon. Instalation  If you are an maven user, add following dependency: <dependency>       < groupId > com.anotherchrisberry < /groupId >       < artifactId > spock-retry < /artifactId >       < version > 0.6.2 < /version >       < type > pom < /type >   </dependency> For gradle users:

Performance Testing – Vegeta Attack!

Performance testing is crucial field of modern software development. Taking into account that in today’s world majority of app’s communication is web-based, it turns out to be even more important. However, it still enjoys less interest than automated functional testing, and publications on load testing subject usually focus on mature and complex tools like JMeter or Gatling. In this post I’d like to introduce command line usage of a super simple and lightweight tool for performance testing of HTTP services, which is Vegeta. Who is Vegeta  Besides Dragon Ball’s character, Vegeta is a simple load testing tool written in GO, that can be used both from command line and as an external library in your project. In order to use Vegeta, download executables from here . It is recommended to set environment variable for ease of use.  For Mac users, you can download vegeta directly from Homebrew repositories: $ brew update && brew install vegeta Vegeta’s Arsenal  Usage