Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
REST Assured
3.
What is BDD?
4.
Implementation of BDD in REST Assured
4.1.
Create a Maven project 
4.2.
GET request in REST Assured
5.
Frequently Asked Questions
5.1.
Is REST Assured a java library or framework?
5.2.
Can we test the SOAP APIs in REST Assured?
5.3.
What makes the REST Assured code human-readable?
5.4.
Does REST Assured support only JSON data format?
5.5.
Do we need to buy the license for REST Assured?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

REST Assured – BDD Style in Rest Assured

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog will help you to implement the BDD style API testing in REST Assured. We will learn the keywords and dependencies required to implement the API testing in REST Assured using the BDD structure and discuss what BDD is

But before we jump into that, let's know about REST Assured first.

REST Assured

REST Assured – BDD Style in Rest Assured

REST Assured is an API testing tool that was developed by Johan Haleby. REST Assured is a Java package we can install in our java project to implement the API testing, validate the response we are getting, and make changes according to the response. REST Assured is also an open-source program implemented in the Groovy programming language.

REST Assured supports all the HTTP methods like PUT, POST, GET, DELETE, and UPDATE. You can also use the headers, cookies, and parameters to validate the response of the HTTP request.
 

You can check out the following blog to learn more about REST Assured.

Introduction to REST Assured

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is BDD?

BDD stands for Behaviour Driven Development, and it is one of the testing methods to test the application's working and to check if it is working according to the owner's expectation. One of the advantages of using the BDD style testing is that the test cases are written in a language readable by everyone included in the application development. 
 

BDD REST Assured

 

BDD removes the barrier between the technical and non-technical teams, helps them to understand the application's working, and makes changes depending on the user's behavior when using the application.

BDD helps to make the developers, stakeholders, and other team members discuss the application behavior depending on the test cases in a common language.

You need to know the following given annotations when writing the test cases in BDD.

  • Given: using given() keyword, we need to define scenarios, or we can say we need to set header information, params, auth, and cookies.
     
  • Whenwhen() keyword tells which statement to execute.
     
  • Then: then() keyword tells the response of the previous statement that was executed.
     

For better understanding, we will implement all these annotations in our code.

Implementation of BDD in REST Assured

Create a Maven project 

Let's make a maven project in IDE. We will be using Eclipse IDE for the implementation, but you can use IntelliJ or any other java based IDE; that's up to you.

First, go to file < project < maven project and then create a maven project by filling in all the necessary information.

maven project
maven project


You need to write the name for your maven project in the group id and artifact id.

maven project


 

Before we discuss the implementation of the BDD style in REST Assured, you need to ensure you have the necessary dependencies in your pom.xml file.
You must add the dependencies in the <dependencies/> tag, and each package will have a separate <dependecy/> tag like the one given below.

pom.xml file

 

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


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


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


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


<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>
</dependencies>


GET request in REST Assured

Now you are ready to implement and execute your REST Assured code in BDD testing.
You need to include the following code in the src/test/java package.


import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

import org.hamcrest.Matchers;

public class sample_test {
  
@Test
public void getrequest() {
given()
.when()
.get("https://ghibliapi.herokuapp.com/films")
.then()
.statusCode(200);
}
}

You can see in the above code that we are implementing all the keywords given, then, and when in the function named getrequest(). Remember that you need to start your code with given() keyword used. To access further keywords.
Include these header files in the code to implement BDD annotations in REST Assured.

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import org.hamcrest.Matchers;

In the above code, we are giving a GET request to the public API https://ghibliapi.herokuapp.com/films, and if the request is successful, we will get the status code 200 in return.

output


You can see in the console our test for the GET request has been passed.

You can perform multiple requests in REST Assured in BDD style. This was one example of implementing the GET request in REST Assured in BDD style.

Frequently Asked Questions

Is REST Assured a java library or framework?

REST Assured is a java based library to test the APIs.

Can we test the SOAP APIs in REST Assured?

We can, but REST Assured does not explicitly support SOAP APIs testing.

What makes the REST Assured code human-readable?

BDD style testing makes the REST Assured code human-readable.

Does REST Assured support only JSON data format?

No, REST Assured supports both XML and JSON data formats.

Do we need to buy the license for REST Assured?

REST Assured is an open-source API testing tool, so you do not need to worry about the license.

Conclusion

This blog taught us about the BDD style testing in REST Assured. We discussed what BDD is in general, and we also learned how to create a maven project and implement a GET request in REST Assured using the BDD style.

To learn more about REST Assured, you can check the following articles.

 

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Builder Pattern in REST Assured
Next article
Let’s Write First POST Request in REST Assured
Live masterclass