Getting Started With Rest Assured

What is Rest Assured?

– Rest Assured is a Java Domain Specific Language which simplifies testing of RESTful Web Services.

– It supports all HTTP Request types like GET, POST, PUT, DELETE, OPTIONS, PATCH and HEAD.

– It can be used to validate and verify the response of different API Requests.

– It allows requests to be constructed with multiple parameters, headers and their body.

– Rest Assured DSL is very similar to any BDD language which makes it simple to use and understand.

– It is an open source project. It is developed/maintained by Johan Haleby and other contributors.

How to setup Rest Assured?

Add the following dependencies to pom.xml:

<dependencies>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>rest-assured</artifactId>
         <version>4.3.2</version>
     </dependency>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>json-path</artifactId>
         <version>4.3.2</version>
     </dependency>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>json-schema-validator</artifactId>
         <version>4.3.2</version>
     </dependency>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>xml-path</artifactId>
         <version>4.3.2</version>
     </dependency>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.13.1</version>
     </dependency>
 </dependencies>

Write a simple test  in Rest Assured

public class BitcoinAPITest {

    @Test
    public void verifyStatusCodeIs200(){
        given().when().get("https://api.coindesk.com/v1/bpi/currentprice.json").then().statusCode(200);
    }
}

Understanding the syntax:

The above test method executes a get request on the API URL (“https://api.coindesk.com/v1/bpi/currentprice.json”) and then verifies whether the status code in the response is equal to 200.

Rest Assured API uses the same syntax as any other BDD language in the form of Given/When/Then format. This syntax is easy to understand and also resolves everything starting from setup, execution and verification in one line.

 

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!