API Testing Using RestAssured Framework
Posted By : Sakshi Dewan Gandhi | 08-Feb-2018
What is Rest-Assured?
Rest Assured is a framework to help you design API Test Scripts. In a maven Project we include the dependency of Rest Assured in the POM.xml and its jar file gets added in the Maven Dependency Folder of a Maven Project.
How to configure Rest Assured in Eclipse or STS?
We have to configure the Rest Assured in STS/ Eclipse or whatever IDE you are wworking with.Rest Assured dependency can be got from its official site io.rest-assured from its Release Notes Section as follows:
Step 1. Goto website io.rest-assured.com.
Step 2. Search for Documentation Option.
Step 3. Click on Release Notes.
Release Notes Page will get opened in Github.
Step 4. Click on the Version you want to work with.
Contents Section gets opened.
Step 5. Click on Highlights link.
Step 6. Copy the <dependency> tag.
Step 7. Paste the dependency tag under the POM.xml of the Maven Project.
How to write API Test?
Now the question arrises that how to generate the API Test Code. For that only the basic knowledge of what a Class is , what a package is and Methods in Java is enough. Now Follow the following steps to Write a Test:
Step 1.Create a New Class inside the Maven Project.
Step 2. Create a New Package you created above.
Step 3. Now import the libraries you would need while accessing a script:
a. io.restassured.RestAssured.
b. io.restassured.response.Response.
So that you could make use of the methods in your script, which are already there in the package.
Step 4.Now create a Public class.
Step 5.Now create a method with Annotation (@Test as we are not going to use any main method and this is our unit testing TestNG framework's annotation) for Testing the Response Code of the API response which you need to validate.
Step 6. Now make use of the method called “get.Statuscode()” in your code. This Method will return the value of the Response code of your API when a Request is made on the same. This is a method of a class "Response" so in order to use its methods we need to make its object.
Step 7. Create an object of the class Response and store the API response as stated above in this object.
Step 8. Assert this Response code with the help of following method:
Assert.assertEquals(code, 200);
Step 9. Print the Code which you have got in the Response for the Test Report.
How to Validate the Output?
Now how to know if the Response Code we are getting is correct. The table of Code can be reffered from wikipidea, in order to validate the code with its response. Mainly you need to check if the response is a success with the Response Code 200.
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
Sakshi Dewan Gandhi
Sakshi has experience into Testing, which comprises of Functional, Performance and End-to end-System Testing. She has worked in various domains like: Telecom, E-Commerce, E-Learning and Language Translation.