REST ASSURED API TESTING

Mani B
4 min readFeb 9, 2021

--

GET METHOD with Key and Values pair combination PART-2

This is my second blog in the series. Part 1 covered GET Method with RestAssured.

In order to test and validate the API with RestAssured sometimes it is needed to have sequential combination keys and values from Json Response of TESTAPI.

I have used the API https://reqres.in/api/users?page=2

In this API with Get Method and status code of 200, this is the Response

{“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!”}}

Now to extract the ID which is basically a Key and Values from this Json Values are First_name and Last_Name and Email. Which is related to a single key Id

.

Advantages: This method is very useful when you want to get the values from API response in KEY -VALUE pair fashion.

Special Features: This method is also used to extract multiple field values associated with a single key with Package org.apache.commons.collections4

This package contains the interfaces and utilities shared across all the sub-packages of this component.

Testing and Validation: Mapping from one key to multiple field values.

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 Marketplace or add a dependency.

3. In the POM.Xml add dependencies for RestAssured and Json .Visit the official page to import those dependencies. I have used RestAssured 4.3.1 also added more dependencies for “Guava-Libraries”.

<dependency>

<groupId>com.google.guava</groupId>

<artifactId>guava</artifactId>

<version>26.0-jre</version>

</dependency>

4. I have used the “Package org.apache.commons.collections4” from here:

https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/package-summary.html

Package org.apache.commons.collections4

This package contains the interfaces and utilities shared across all the sub-packages of this component.

I used the interface MultiMap .<Keys, Values>

<Keys> Reference Keys

<Values> Associated with reference keys.

Used this Deprecated.

since 4.1, use MultivalueMap instead.

@Deprecatedpublic interface MultiMap<K,V>extends IterableMap<K,Object>

**Defines a map that holds a collection of values against each key.

A MultiMap is a Map with slightly different semantics. Putting a value into the map will add value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

Reference: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html

My POM.XML looks like this with added dependencies.

<dependencies>

<! — https://mvnrepository.com/artifact/io.rest-assured/rest-assured

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-collections4</artifactId>

<version>4.2</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-exec</artifactId>

<version>1.3</version>

</dependency>

<dependency>

<groupId>com.google.guava</groupId>

<artifactId>guava</artifactId>

<version>26.0-jre</version>

</dependency>

<dependency>

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

<artifactId>rest-assured</artifactId>

<version>4.3.1</version>

<scope>test</scope>

</dependency>

<dependency>

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

<artifactId>json-path</artifactId>

<version>4.3.3</version>

<scope>test</scope>

</dependency>

<dependency>

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

<artifactId>xml-path</artifactId>

<version>4.3.3</version>

<scope>test</scope>

</dependency>

Code Breakdown:

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

· String mystringid =myresponse.jsonPath().getString(“data.id”);

o I have stored all the id from API in mystringid.

· String myfirst_name =myresponse.jsonPath().getString(“data.first_name”);

· String mylast_name=myresponse.jsonPath().getString(“data.last_name”);

· String myemail =myresponse.jsonPath().getString(“data.email”);

6.To split the string at “,”.

String str[] =myfirst_name.split(“,”);

String str1[] =mylast_name.split(“,”);

String str2[] =myemail.split(“,”);

String str3[]= mystringid.split(“,”);

7.Converted String Array to subsequent List in these easy steps.

List<String> flist = Arrays.asList(str);

List<String> llist = Arrays.asList(str1);

List<String>emaillist=Arrays.asList(str2);

8 Now we have the list of Firstname and Lastname and Email.Used this MultivalueMap to map each key to different field Values.

Deprecated Version 4.1

MultiValueMap<String,String>hmap=new MultivalueMap<String,String>();

9.Used this for 6 records of each Id,Firstname and Last name and Email.

Method: Hmap.put(key,Value) This is to place values with key and vale in the Hmap.

Hmap.put(key,Value) is used and in the values for firstname list and Lastname List and Emaillist is passed.

For Loop.

Here in the API 6 Records Each for Keys and Values.

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

{

hmap.put(str3[i].trim(),flist.get(i));

hmap.put(str3[i].trim(),llist.get(i));

hmap.put(str3[i].trim(),emaillist.get(i));

}

The Full code for methodKeymappedtoMultipleValues is here:

GET METHOD with Single Key and multiple Values

I have used these imports in my method.

My Console-Output is :

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

See you in other methods of API Testing!

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

https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MultiMap.html

Mani Bhute

Completed on 02/09/2021

--

--

Mani B
Mani B

Written by 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.

No responses yet