Skip to main content

REST-Assured: going deeper

In my previous post I described the basic REST-Assured usage – the lightweight framework for testing RESTful services. Despite the fact that described range of functionalities would be enough in most cases,REST-Assured has a lot more to offer. In this post I would like to bring some more advanced examples of the framework usage.



I recommend you also my other posts about REST-Assured and building microservice’s test automation frameworks:

  • REST-Assured – framework overview
  • Building microservices testing framework

Object Mapping 

Sending request’s body as string is easy and straightforward, but it can be inconvenient in the case of more complex operations on request / response properties. Proven solution for this is a good-known serialization of request/response body to objects. REST-Assured supports object mapping to (and from) JSON and XML. For JSON you need either to have Jackson or Gson in your classpath and for XML you need JAXB. Here is an example of request object serialization using Jackson library. Our object represents the following class:


If we have either Jackson or Gson in out classpath, we can create new UserDto object, and send it as a body to REST-Assured method:


What happend here is we’re creating new UserDto object, and after passing it to body() method, Jackson/Gson serialize it to following json:



JSON Schema 

Validation Main idea behind JSON Schema Validation is to compare response correctness with declared schema. Assume that you have the following response (this example is from my wiremock’s post):


What we want to do is to perform structural validation, comparing your response with schema declared in your project. First things first, we have to add JSON-schema dependency to our project. In case of gradle it would be:


testCompile group: ‘com.jayway.restassured’, name: ‘json-schema-validator’, version: ‘2.5.0’

Second, declare schema definition in json file. Below you can see simple example for checking presence of response properties and types:


Last thing is to add static import to class, where you want call JsonSchemaValidator methods (obviously you can avoid star import by providing specific method name):


import static com.jayway.restassured.module.jsv.JsonSchemaValidator.*;

After it’s done, you can call JsonSchemaValidator inside your REST-Assured methods:


After calling /user/{id} method, REST-Assured invokes json schema’s matchesJsonSchemaInClasspath() method, witch compares response against schema definition previously declared in project.


Complex Response 

Validation Since REST-Assured is written in Groovy, it takes advantage of Groovy’s sytax. One of the coolest thing in groovy is it’s collection API. You can check all the groovy collection’s interface details here. For the REST-Assured part, let’s assume that we have a following response body:


Our task would be to check, if object which price is greater than 18.00 has title equals to “Sabbath Bloody Sabbath”. Let’s see the code for that:



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 

In this short post my goal was to introduce you with some more advanced REST-Assured examples. Of course you can limit your test only to simple flow described in my previous post and that’s perfectly fine, but I strongly encourage you to expand your test with examples from this post. Remember, that those are only few of my favourites, for more – check framework’s documentation.

Popular posts from this blog

Notes after TestingCup 2018

On May 28-29th I attended TestingCup conference in Łódź. Having quite unique perspective: this was my second year in row as a Speaker at this conference I want to share some thoughts on the event. Dust has settled, lets go! 



Championship Originally TestingCup is a software testing championship. Wait, what? Yes, the formula is unique: teams and individuals from all around Poland are competing in finding the most bugs and defects in specially prepared application - Mr. Buggy. I don’t have specific data, but since this year’s conference was all english I guess competitors were not only from Poland. As a spectator, I must say that the whole competition looked very professional. There were team shirts and names, podium and trophies (gold cup and cash). 
Some cons? Testing championship is held at the first day of the conference. So this is already a conference, but if you’re not taking part in the championship… there’s not much to do, since all the talks are in the next day. Organizers are aw…

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 deeperBuilding 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 m…

Building microservices testing framework

RESTful services and microservice architecture in general are big trends in the industry right now. I’ve noticed it also from this blog’s traffic analytics, where topics related with REST testing get the highest interest. Therefore I’ve decided to create some kind of example framework for RESTful APIs functional testing. Project is based on REST-Assured as a services client, Spock as a testing framework, Groovy and Gradle. You can clone full repository from my github. Tests are run against Wiremock API described in this post. Please consider this project as a kind of bootstrap, since it’s an example, not full-blown test framework. Ok, so as Linus said – talk is cheap, show me the code!
Dependencies Usually, first thing for me after importing new project is opening build.gradle (or pom.xml, in case of maven). In our example, the most important dependencies are REST-Assured and Spock. We’ve also Jackson, for serializing objects, Logback for our logs and snakeyaml for reading .yaml files,…