Getting started with REST Assured

Hansani Jayasekara
Oct 14, 2019 · 7 min read

Hi guys, it’s my first blog and I am going to help you to write your first REST Assured test. First of all, let me explain you what Rest Assured is.

1. What is REST Assured.

REST Assured is a Java library for validation of REST web services developed and maintained by Johan Haleby with the support of several other contributors over the years. Testing and validating REST services in Java is tougher than doing it with a dynamic language such as Ruby and Groovy. REST Assured has given the easiness of using these languages in Java domain. It provides the luxury of Integrating the tests with prevailing Java based testing frameworks such as Junit, TestNG and Selenium Webdriver.

Now we all know what REST Assured is and let’s move on and check what are the benefits of using REST Assured.

2. Benefits of REST Assured

1. REST Assured reduces the need for writing same section of code many times, which is required to set up an HTTP connection, send a request and receive and parse a response.

2. It supports for BDD (Behavior-Driven Development) by using test notation, Given, When and Then, which makes the tests human readable.

3. Since REST Assured is a Java library, integrating it into a continuous integration / continuous delivery setup is an easy when combined with a testing framework such as JUnit or TestNG.

4. It provides interesting features such as,

a. Measuring Response Time

b. Validate Response Time,

c. XML Response Verification,

d. JSON Response Verification, etc

3. How does REST Assured work

REST Assured provides end to end support in developing a test by providing various methods to achieve following steps in a test.

a. Format the HTTP request

b. Send it over a network

c. Validating HTTP status code of a request

d. Validating response data.

Ok folks, now its time to create your first REST Assured project

4. Creating a simple REST Assured project

4.1 Pre-requisites

In order to start your project, you should have following installed and configured in your computer.

JDK 8

Maven 3.5.x

Eclipse IDE

4.2 Steps

Lest’s get started with creating the project.

4.2.1 Creating a New Project in Eclipse

Let’s get started with creating the project. Open Eclipse IDE and create a new project.

  1. Click on new –> Other –> Maven –> Maven Project → Next

2. Set the work space and click on next

3. Select the archetype as maven-archetype-quickstart and click on next

4. Provide details Artifact id, Group id, name and description. and click on Finish.

4.2.2 Configure TestNG

1. In the pom.xml file add the rest assured dependency for maven

URL- https://mvnrepository.com/artifact/org.testng/testng

Latest TestNG Maven dependency as of now

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>

2. Create the test suite XML file to call the test class and place the following code. e.g. testng.xml in src\test\resources folder

<suite name = "Restassured">
<test name = "Sample Test">
<classes>
<class name = "com.restassured.test.SampleTest" />
</classes>
</test>

3.Set the Surefire Plugin configuration to run the xml file

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId><version>2.20</version>
<configuration>
<suiteXmlFiles><suiteXmlFile>src\test\resources\testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

4.2.3 Configure REST Assured

In the pom.xml file add the rest assured dependency for maven

URL — https://mvnrepository.com/artifact/io.rest-assured/rest-assured

Latest Rest Assured Maven dependency as of now.

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>

Write the test method

1. Create a class named “SampleTest.java”

2.Add the following method to the class

@Test

3. Run the test project using mvn install command

Congratulations !!!!! You created your first REST Assured test ! :) :) :)

Now we are going to explore some interesting features of REST Assured.

5. Few interesting features of REST Assured

5.1 BDD supported syntax

REST Assured provides BDD (Behavior Driven Development) supported format. It helps to write cleaner test codes which are human readable. In the request the pre-conditions are listed under the “Given” section. The action to perform is given under the “When” section. Finally, the verification is given under “Then” section

5.2 Handling parameters

Path Parameters

Assume you are to make a GET call with a path parameter.

@Test

Query Parameters

To send query parameter with the GET call

@Test

Form Parameters

To send form parameter with the POST call

@Test

5.3 Validate the status code

Let’s assume that we have to validate whether the status code is 200. You can simply achieve that

@Test

5.4 Get the response time

If we have to measure the response time, we can achieve that using following code segment. You can get the response time in a specific time unit as desired.

@Test

5.6 Validate the Response time

There can be situations where you want to validate the response time. You can simply validate whether the response time is below a predefined value using following method

@Test

5.7 XML response validation

Please refer the following XML response

<shopping>
<shops>
<shop category="grocery">
<name>My Store</name>
<scale>Small</scale>
<place>Nugegoda</place>
</shop>
</shops>
<category type="food">
<item>Rice</item>
<item>dhal</item>
</category>
<category type="Stationery ">
<item>Paper</item>
<item quantity="4">Pens</item>
</category>
</shopping>
  1. Assume you are to validate whether the store name returned is correct, you can achieve that using following code.
@Test

2. Assume you are to validate whether the items under ‘food’ category are correctly given, you can achieve it simply using following.

@Test

3. Similarly, multiple values can be validated at the same time.

@Test
public void validateStoreProperties() {

5.7 JSON response validation

Please refer the following JSON response

{  
"mystore":{
"book":[
{
"author":"Dan Brown",
"category":"fiction",
"price":590,
"name":"Angels & Demons"
},
{
"author":"Carolyn Keene",
"category":"mystery",
"price":420,
"name":"Nancy Drew : The Secret of the Old Clock"
},
{
"author":"J. R. R. Tolkien",
"category":"fiction",
"isbn":"0-395-19395-8",
"price":650,
"name":"The Lord of the Rings"
},
{
"author":"Meg Cabot",
"category":"young adult novels",
"isbn":"0-553-21311-3",
"price":560,
"name":"The Princess Diaries"
}
]
}
}
  1. Assume you are to validate whether the response contains all the books
@Test

2. Assume you are to validate that the names of the books with a price greater than 500 correct

@Test
public void validateJason() {

5.8 Extract response data

5.8.1 From JSON response

WE can do that in two ways

  1. Using built in REST Assured functionalities
  2. Using JSON path

Using built in REST Assured functionalities

  1. To extract the whole response as string
@Test
public void extractValues(){

2. To extract a specific value, you can use the following code. Let’s assume that you are to extract the author of the first book

@Test
public void extractSpecificValues(){

3. To extract a multiple value, you can use the following code. Let’s assume that you are to extract the author of the first book, author of the second book and the Name of the third book.

@Test
public void extractMultipleValues(){

Using JSON path

In order to use json-path functionalities you need to include the json-path maven dependency in the pom.xml file.

The latest json-path maven dependency as of now

<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
  1. Let’s assume that you are to extract the categories of the books.
@Test
public void extractValueswithJsonPath (){

5.8.2 From XML response

Using built in REST Assured functionalities

1.To extract the whole response as string

@Test
public void extractValueswithXmlPath (){

Using XML path

In order to use XML-Path functionalities you need to include the XML-Path maven dependency in the pom.xml file.

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>3.0.7</version>
<scope>test</scope>
</dependency>

Let’s assume that you are to extract the place of the store.

@Test
public void extractValueswithXmlPath1 (){

References

  1. http://rest-assured.io/
  2. https://github.com/rest-assured/rest-assured/wiki/Usage

Aeturnum

Aeturnum is a software services organization based in…