Table of contents
1.
Introduction 
2.
Introduction to Rest Assured
3.
Benefits of Rest Assured
4.
Set Up Project
5.
Write First Get REST Assured Test
5.1.
Explanation
5.2.
Run the Project
5.3.
Result
6.
Frequently Asked Questions
6.1.
What is an API(Application Programming Interface)?
6.2.
What is Rest Assured?
6.3.
Where is REST assured used?
6.4.
What are the different dependencies required for REST Assured testing?
6.5.
What is TestNG?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Let’s Write First GET REST Assured Test

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Hello Coder!! Working with API(Application Programming Interface) mainly includes four methods. These methods are: GET, PUT, POST, and DELETE. In this article, we will discuss what REST Assured is. What are the benefits of using REST assured in API testing?   

introduction

After a brief introduction to terminologies, we will study the process of setting up the project for testing and, finally, how to write first GET REST assured test.

Introduction to Rest Assured

REST Assured is an open-source and Java Domain-Specific Language. It was developed and maintained by Johan Halaby. It allows us to write readable, maintainable, and robust tests in Java for RESTful APIs. The test for REST Assured runs on existing unit testing frameworks like TestNG or JUnit. It extracts a lot of boilerplate code and makes your tests powerful and easy to read and maintain.    

Rest assured

To use REST assured in your project, you need to add the below-mentioned dependencies in your project. 

<dependencies>
  <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version><--Enter the latest version here--></version>
      <scope>test</scope>
  </dependency>
</dependencies>

Benefits of Rest Assured

Some of the benefits of using REST Assured include:

  • Provides support for all known HTTP(HyperText Transfer Protocol) methods like GET, PUT, POST, PATCH, DELETE, etc. 
     
  • Uses Hamcrest Matchers to enable you to write checks in a human-readable language.
     
  • Helps in removing a lot of boilerplate code for setting up an HTTP connection, sending a request, and receiving and parsing a response.
     
  • It supports a GWT (Given/When/Then) notation for testing, which instantly makes your tests human-readable.
     
  • Since REST Assured is a Java library, integrating it into a continuous integration / continuous delivery set-up is a breeze, especially when combined with a Java testing framework such as JUnit or TestNG.
     

To understand these benefits, let's write first Get Rest Assured test. 

Set Up Project

  • Click on this link and download Eclipse.
  • Select Eclipse IDE for Java Developers. 
eclipse installer
  • Wait for the installation to complete. It may take a few minutes. 
installation
  • After installation, select your work directory.
select workdirectory
  • Go to file > new > Project.
create new project
  • Create a Maven project.
Select project wizard
  • Click next and fill in details and leave others as default.
new maven project
  • The Eclipse will create a Maven project. The window will look alike:
pom.xml file
  1. Go to the help button in the navigation bar.
  2. Click on install new software.
  3. Go to https://github.com/cbeust/testng-eclipse/.
  4. Copy the URL of the latest release of the testNG plug-in.
  5. Add the URL in front of the Work With option. And click Add. 
  6. This will add the TestNG plug-in to Eclipse.
install TestNG plugin
  • As the set-up is complete, you can write first Get REST assured test. 

Write First Get REST Assured Test

  • Go to src/test/java folder > right-click on it and click on the ‘package’ option.
  • Give a package name (say tests). 
  • Click ‘FINISH’ to create this package. 
create new package
  • Under this test package, create a new class. By right-clicking on the ‘tests’ package. 
  • Give the class name as GetTest and click Finish.
create new class
  • Create a function and add annotations (comment to explain) using TestNG. 
  • We will be using a public API. For that, just google "Rest API for testing," and you will get plenty of options. In this article, we will use the GET API of Reqres.in website.
package tests;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
 
public class GetTest {
@Test
	public void get_test()
	{
		Response response = RestAssured.get("https://reqres.in/api/users?page=2");
		System.out.println(response.getStatusCode());
		System.out.println(response.getTime());
	}
}
You can also try this code with Online Java Compiler
Run Code

Explanation

In the above code, we have created a method 'get_test()' to write first Get Rest Assured test. In this method, we created a response object of the 'Response' class to store the result of the GET request. 

Later we used the 'get()' method of the 'RestAssured' class and passed the public API URL as a parameter. All the results are stored in the 'response' object. 

To get the Status Code of the API, we used the 'getStatusCode()' method of the 'Response' class.

To get the time taken, we used the 'getTime()' method of the 'Response' class. 

Run the Project

  • To run just right-click on the ‘GetTest.java’ file.
  • Click RUN As TestNG test.
run as testNG test

Result

Output

Now let’s see some frequently asked questions.

Frequently Asked Questions

What is an API(Application Programming Interface)?

An application programming interface is a type of software interface, that offers a service to other pieces of software.

What is Rest Assured?

REST Assured is a Java DSL, or Domain-Specific Language, that allows you to write powerful, readable, and maintainable tests in Java for your RESTful APIs.

Where is REST assured used?

Rest Assured is used to validate REST APIs using the Java library. The Java library acts as a headless client to interact with Rest web services. It is based on the Rest Assured library and can validate the server's HTTP responses.

What are the different dependencies required for REST Assured testing?

The various dependencies required for testing REST assured are: Rest assured, Rest Assured Common, Rest Assured All, testNG, and json-path. 

What is TestNG?

TestNG is a testing JUnit and NUnit framework. It supports tests configured by annotations, data-driven testing, parametric tests, etc.

Conclusion

In this blog, we have learned how to write first Get REST assured test. The articles contain an introduction to REST Assured and its benefits. The steps to set up Rest Assured to write first Get Rest Assured test. At last, we discussed the process of writing the test. 

If you found this blog has helped you enhance your knowledge, and if you want to learn articles like write first Get REST assured test, check out our articles below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass