top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Building API tests with REST Assured: A Quick Step by Step Start Guide - Part 2

This blog is a continuation of part 1. I am posting the link for part 1 here, https://www.numpyninja.com/post/building-robust-api-tests-with-rest-assured-a-quick-step-by-step-start-guide , you can go through the part 1 and come to this part.


Let’s create our first REST Assured, testNG tests. The endpoint we are using is

you can get dummy endpoints from reqres.in


1. Let's create a Class

  1. a) Open Eclipse, expand our project RESTAssuredDemoProject




  1. b) Right click on src/test/java, go to File, New click on package, create a new package called demoTests




  1. c) Click on Finish



  1. d) Right click on package demoTests, go to File, New , click on Class, create a new Java class called Test01



1. e) Click on Finish




2. Create a method, annotate with testNG


2.a) Create a method


public void test()

{


}


Annotate with @Test, it will show an error. So, you can hover over it, it will show Import ‘Test’ (org.testng.annotations), click on it, it will import testNG annotation import org.testng.annotations.Test;




3. GET Request and validation

Now type in Rest without completing the full word, followed by control + spacebar, it will show auto suggestions of the import, so click on io.rest assured. Since we had added all the libraries and jars earlier it will be available and import io.restassured.RestAssured; will be done automatically




As we are doing Get Requests for API, we have to give .get(), followed by url of the endpoint and store it in a response variable. Inorder to get the Response, also import the suggested import, import io.restassured.response.Response;





We can get responses using response.getStatusCode(), response.header("content-Type"), response.getStatusLine() and so on, after that we are printing it in the console using System.out.println();






We can use assertion for validation of the status code, Assert.assertEquals(response.getStatusCode(), 200);

hoover over Assert, it will import org.testng.Assert. The HTTP status code is the numerical value indicating the Response's status, here status code 200 is for success.





4. Run using TestNG test




This result will be displayed in our console output with status code 200 and body of the API, which shows the test results Passed.





So, we have successfully ran our first testNg test.


Conclusion:

RestAssured provides a simple and intuitive syntax for making HTTP requests to RESTful APIs. Its syntax closely resembles natural language, making it easy to read and write test cases.

RestAssured allows us to write expressive and easy-to-understand assertions for API responses. We can check response status codes, headers, response body content, and more, making it effective for validating the correctness of API responses.

It offers extensive options for configuring requests, including specifying request headers, query parameters, request bodies, and authentication methods. This flexibility is essential when working with various API endpoints.






122 views0 comments
bottom of page