API Testing with Rest Assured Framework

Posted By : Divya Varnwal | 28-Aug-2019

 Rest assured is a java library through which perform API test on the level. Rest Assured is an open-source automation tool with an integration of TestNG and Junit.It is developed by the JAY WAY company. It provides a domain-specific language for writing strong and maintainable tests for RESTful APIs.

 

Rest assured is used for testing the Restful web service APIs.Rest assured Library provides various way to validate the HTTP responses which are received from the server. For example, we can validate the response code, JSON object, and header. It is a very flexible library for developing and testing the customize business logic for the APIs.


 

What is a RESTful web service?

 

There are two types of web services.

  1. SOAP web service

  2. REST web service

 

Soap (Simple Object Access Protocol) is  XML based that defines standard communication protocol between two different web applications.

 

is JSON (JavaScript Object Notation) based that also defines standard communication protocol between two different webs. The rest is Rest (Representational State transitional)faster and more useful web service nowadays. 


 

Now API Testing by using RestAssured Framework:

 

By using a rest assured framework, We can test all APIs like GET, POST, PUT, DELETE, etc.

 

Here we are Testing GET API with the help of Rest assured framework:

 

Step1:  Create a Maven project in Eclipse.

Step2:  Add dependency XML code in the  POM.XML file


dependecies
 
RestAssured:
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
               <version>3.0.0</version>
               <scope>test</scope>
</dependency>
 

TestNG:

 
.restassured.RestAssured;
	import io.restassured.response.Response;
	import io.restassured.specification.RedirectSpecification;
	import io.restassured.specification.RequestSpecification;

	public class GetAPITest {
		
		
		@Test
		public void getmethod() 
		
		{
			
		
			RequestSpecification request = RestAssured.given();
			request.header("Content-Type", "Application/json");
			Response code = RestAssured.get("https://reqres.in/api/users?page=2");
		    int statuscode = code.getStatusCode();
		    Assert.assertEquals(statuscode, 200);
		    System.out.print("Status code is"+statuscode);
				
			
		}
 

Step5: Run the class then get the result on the console.


Result
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>

JSON:

Class

Step3: Create a class “GetAPITest” under src/test/java.

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20190722</version>
</dependency>
 
Related Tags

About Author

Author Image
Divya Varnwal

Divya is a part of QA team. She is a good learner enthusiastic and dedicated towards her work. She likes to spend her time roaming with friends and listening to songs.

Request for Proposal

Name is required

Comment is required

Sending message..