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.
-
SOAP web service
-
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](https://s3.amazonaws.com/oodles-blogs/blog-images/7fbdeb5b-d9cf-4987-9ec8-79f780dc860e.png)
<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](https://s3.amazonaws.com/oodles-blogs/blog-images/ecda49ee-bbc0-4fad-bf04-aa14fc3009fe.png)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
JSON:
![Class](https://s3.amazonaws.com/oodles-blogs/blog-images/70c6915b-81e8-4387-8b0e-a1d387881538.png)
Step3: Create a class “GetAPITest” under src/test/java.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.