Rest Assured API Testing Methods-part1-Get Request

Mani B
6 min readJan 30, 2021

REST ASSURED API TESTING GET METHOD PART-1

To test and validate REST API for JAVA based framework, RestAssured is the solution. RestAssured is simple to test and validate the responses from REST API.

RestAssured contains the JAVA library DSL to test REST based API and its on HTTP builder so when we get the Json response by sending the HTTP request ,we can use different methods like GET,PUT,DELETE,OPTIONS ,PATCH ,HEAD to validate the response.

Advantages to use with JAVA: It’s very easy to integrate with testing framework like TestNG, Junit.

Special Features: RestAssured uses the package Package io.restassured and class RestAssured extends to Object(Super class) .

Class object (Super class) is a Parent class so all objects, including arrays, implement the methods of this class.

We can use Java’s collection too with framework to test and validate.

While using it in the project we import the package to implement those methods which is in the package with our response objects.

Testing and Validation :

Tools required :JAVA ,Eclipse IDE Or Intelli J with Maven and TestNG Plugin.

Steps to execute:

1.Create a Maven Project in the Eclipse IDE.

2.TestNg plug in must be installed. You can install from Market place or add dependency.

3.In the POM.Xml add the dependencies for RestAssured and Json .Visit the official page to import those dependencies. I have used RestAssured 4.3.1.

Maven dependency for RestAssured to be added to validate

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>4.3.1</version>

<scope>test</scope>

</dependency>

Maven dependency for Json to be added to validate Json response

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>json-path</artifactId>

<version>4.3.3</version>

<scope>test</scope>

</dependency>

4. Save the Pom.xml after adding the above dependencies and Update the Maven Project.

5.Ready to create the test cases.

I have used API https://reqres.in/ for testing and validation purpose.

HTTP has GET,PUT,PATCH,DELETE methods .

GET Request : In this URL for Get Request ,it has these many APIs.

LIST USERS,SINGLE USER ,SINGLE USER NOT FOUND,

LIST RESOURCES,SINGLE RESOURCE,RESOURCE NOT FOUND ON

LIST USERS API https://reqres.in/api/users?page=2

To validate the API for status code :200

TESTCASE .1 TC_GetUsersFirstNames() : To extract all the Status code and total no of id and extract all first_names from page2.

*******“Get Method for Name of User exist on page2”*******

Project Specification:

In this project ,I have created an object of Response class and used Get Status code method.Used many test cases so used TestNG s priority feature so can assign priorities to execute those test case which you want in a sequence.

TESTCASE .2 TC_ CheckUserInputName(): To accept random first_name from user to check if its in the API’s Page2 and index no.

Get Method from user input to check if its exist on page2.

TESTCASE.3 TC_GetResourcAssert() This method is to validate on page 2 the status code is 200.

Get Method to validate response and status code 200

As Get Request will always have statuscode :200.

TESTCASE.4 TC_UserNotFound() This method shows the status code is 404

TESTCASE4.

Validating the response for Status Not Found

TESTCASE.5 TC_GetResourceRequest()

This method shows the data like name,color,pantone_value which is greater than year 2001.

Get Method for Yeargreater than 2001 extract name ,color,Panotone_value

The GetRequest with all API from https://reqres.in/

https://reqres.in/api/users?page=2

https://reqres.in/api/users/2

https://reqres.in/api/users/23

https://reqres.in/api/unknown

https://reqres.in/api/unknown/2

https://reqres.in/api/unknown/23

*******************************************************************

The complete Code for testing and validating Get Request is:

package Getrequest;

import static org.testng.Assert.assertEquals;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

import io.restassured.http.ContentType;

import io.restassured.response.Response;

public class RestAssuredValidate{

@Test(priority=1)

public void TC_GetUsersFirstNames()

{

Response response=RestAssured.get(“https://reqres.in/api/users?page=2");

System.out.println(“Status code is:”+response.getStatusCode());

System.out.println(“total id is:”+response.jsonPath().getList(“data.id”).size());

System.out.println(“total first name is:”+response.jsonPath().getList(“data.first_name”).size());

List<String>Names=new ArrayList<String>();

Names=response.jsonPath().getList(“data.first_name”);

for(int i=0;i<6;i++)

{

System.out.println(“The first names are these:”+Names.get(i).toString());

}

}

@Test(priority=2)

public void TC_CheckUserinputName()

{

Response response=RestAssured.get(“https://reqres.in/api/users?page=2");

Scanner sc=new Scanner(System.in);

System.out.println(“Enter the First Name:”);

String mystring=sc.nextLine();

System.out.println(“I am checking Firstname as”+mystring);

List<String>Names=new ArrayList<String>();

Names=response.jsonPath().getList(“data.first_name”);

int foundAtIndex = -1;

for(int i=0;i<6;i++)

{

if(Names.get(i).toString().equals(mystring))

{

foundAtIndex = i;

if(foundAtIndex >=0)

System.out.println(“The Firstname matched from page2 at index”+(foundAtIndex+1));

}

}

}

@Test(priority=3)

public void TC_GetResourcAssert()

{

System.out.println(“I am in GetRequest”);

RestAssured.baseURI=”https://reqres.in/api/users?page=2";

RestAssured.given().param(“page”,”2").when().get().then().assertThat().log().all().statusCode(200);

}

@Test(priority=4)

public void TC_UserNotFound()

{

Response response=RestAssured.get(“https://reqres.in/api/users/23");

int Statuscode=response.getStatusCode();

System.out.println(“The status code for Not Found is “+Statuscode);

}

@Test(priority=5)

public void TC_GetResourceRequest()

{

Response response=RestAssured.get(“https://reqres.in/api/unknown");

List<Integer>Yearsmade=new ArrayList<Integer>();

Yearsmade=response.jsonPath().getList(“data.year”);

for(int i=0;i<6;i++)

{

System.out.println(Yearsmade.get(i).intValue());

int year=Yearsmade.get(i).intValue();

if(year>=2001)

{

System.out.println(response.jsonPath().getString(“data.name”));

System.out.println(response.jsonPath().getString(“data.color”));

System.out.println(response.jsonPath().getString(“data.pantone_value”));

}

}

}

}

Console Output is :

[TestNG] Running:

C:\Users\manib\AppData\Local\Temp\testng-eclipse — 1864487347\testng-customsuite.xml

Status code is:200

total id is:6

total first name is:6

The first names are these:Michael

The first names are these:Lindsay

The first names are these:Tobias

The first names are these:Byron

The first names are these:George

The first names are these:Rachel

Enter the First Name:

Tobias

I am checking Firstname asTobias

The Firstname matched from page2 at index3

I am in GetRequest

HTTP/1.1 200 OK

Date: Fri, 29 Jan 2021 22:32:41 GMT

Content-Type: application/json; charset=utf-8

Transfer-Encoding: chunked

Connection: keep-alive

Set-Cookie: __cfduid=deaa7c25bfa5ad267b451f7c3ca9df1611611959561; expires=Sun, 28-Feb-21 22:32:41 GMT; path=/; domain=.reqres.in; HttpOnly; SameSite=Lax; Secure

X-Powered-By: Express

Access-Control-Allow-Origin: *

Etag: W/”406-ut0vzoCuidvyMf8arZpMpJ6ZRDw”

Via: 1.1 vegur

Cache-Control: max-age=14400

CF-Cache-Status: HIT

Age: 5491

cf-request-id: 07f1e006f4000007ceb7a8a000000001

Expect-CT: max-age=604800, report-uri=”https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"

Report-To: {“endpoints”:[{“url”:”https:\/\/a.nel.cloudflare.com\/report?s=YbtDtJYkry%2BwAiIF07KtVzT6CoJem14PcJIv5EPGavmAY%2FZHulE6bTC%2BLAWJ0G8u92hrlUUyNLNMwQZGEMGqt8rffJyZFVVvI4Q%3D”}],”group”:”cf-nel”,”max_age”:604800}

NEL: {“max_age”:604800,”report_to”:”cf-nel”}

Vary: Accept-Encoding

Server: cloudflare

CF-RAY: 6196691e5c7707ce-ATL

Content-Encoding: gzip

{

“page”: 2,

“per_page”: 6,

“total”: 12,

“total_pages”: 2,

“data”: [

{

“id”: 7,

“email”: “michael.lawson@reqres.in”,

“first_name”: “Michael”,

“last_name”: “Lawson”,

“avatar”: “https://reqres.in/img/faces/7-image.jpg"

},

{

“id”: 8,

“email”: “lindsay.ferguson@reqres.in”,

“first_name”: “Lindsay”,

“last_name”: “Ferguson”,

“avatar”: “https://reqres.in/img/faces/8-image.jpg"

},

{

“id”: 9,

“email”: “tobias.funke@reqres.in”,

“first_name”: “Tobias”,

“last_name”: “Funke”,

“avatar”: “https://reqres.in/img/faces/9-image.jpg"

},

{

“id”: 10,

“email”: “byron.fields@reqres.in”,

“first_name”: “Byron”,

“last_name”: “Fields”,

“avatar”: “https://reqres.in/img/faces/10-image.jpg"

},

{

“id”: 11,

“email”: “george.edwards@reqres.in”,

“first_name”: “George”,

“last_name”: “Edwards”,

“avatar”: “https://reqres.in/img/faces/11-image.jpg"

},

{

“id”: 12,

“email”: “rachel.howell@reqres.in”,

“first_name”: “Rachel”,

“last_name”: “Howell”,

“avatar”: “https://reqres.in/img/faces/12-image.jpg"

}

],

“support”: {

“url”: “https://reqres.in/#support-heading",

“text”: “To keep ReqRes free, contributions towards server costs are appreciated!”

}

}

The status code for Not Found is 404

2000

2001

[cerulean, fuchsia rose, true red, aqua sky, tigerlily, blue turquoise]

[#98B2D1, #C74375, #BF1932, #7BC4C4, #E2583E, #53B0AE]

[15–4020, 17–2031, 19–1664, 14–4811, 17–1456, 15–5217]

2002

[cerulean, fuchsia rose, true red, aqua sky, tigerlily, blue turquoise]

[#98B2D1, #C74375, #BF1932, #7BC4C4, #E2583E, #53B0AE]

[15–4020, 17–2031, 19–1664, 14–4811, 17–1456, 15–5217]

2003

[cerulean, fuchsia rose, true red, aqua sky, tigerlily, blue turquoise]

[#98B2D1, #C74375, #BF1932, #7BC4C4, #E2583E, #53B0AE]

[15–4020, 17–2031, 19–1664, 14–4811, 17–1456, 15–5217]

2004

[cerulean, fuchsia rose, true red, aqua sky, tigerlily, blue turquoise]

[#98B2D1, #C74375, #BF1932, #7BC4C4, #E2583E, #53B0AE]

[15–4020, 17–2031, 19–1664, 14–4811, 17–1456, 15–5217]

2005

[cerulean, fuchsia rose, true red, aqua sky, tigerlily, blue turquoise]

[#98B2D1, #C74375, #BF1932, #7BC4C4, #E2583E, #53B0AE]

[15–4020, 17–2031, 19–1664, 14–4811, 17–1456, 15–5217]

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\Default suite\Default test.xml

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\Default suite exists: true

PASSED: TC_GetUsersFirstNames

PASSED: TC_CheckUserinputName

PASSED: TC_GetResourcAssert

PASSED: TC_UserNotFound

PASSED: TC_GetResourceRequest

===============================================

Default test

Tests run: 5, Failures: 0, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 5, Failures: 0, Skips: 0

===============================================

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\toc.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\Default test.properties

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\index.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\main.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\groups.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\classes.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\reporter-output.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\methods-not-run.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite\testng.xml.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\Default suite exists: true

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\old\index.html

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\old exists: true

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@49e4cb85: 21 ms

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms

[TestNG] Time taken by org.testng.reporters.XMLReporter@36d64342: 7 ms

[Utils] Attempting to create C:\Users\manib\eclipse-workspace\RestAssured\test-output\junitreports\TEST-Getrequest.RestAssuredValidate.xml

[Utils] Directory C:\Users\manib\eclipse-workspace\RestAssured\test-output\junitreports exists: true

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@19dfb72a: 4 ms

[TestNG] Time taken by org.testng.reporters.jq.Main@3ac3fd8b: 26 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@30c7da1e: 4 ms

Let me know your feedback and what would you like hear more.

See you in other methods of API Testing!

Reference: Rest Assured.io(https://rest-assured.io/)

Mani Bhute

Completed on 01/29/2021

*************************************************************************************************************************************

--

--

Mani B

Mani has done Masters in Electronics and currently working in IT Automation SDET .Her area of specialization in Java ,Selenium ,Katalon, REST API ,RestAssured.