Do you think IIT Guwahati certified course can help you in your career?
No
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?
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.
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.
Copy the URL of the latest release of the testNG plug-in.
Add the URL in front of the Work With option. And click Add.
This will add the TestNG plug-in to Eclipse.
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.
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 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
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.
Result
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: