DataDrivenTesting Using HashMap in RestAssured

RESTFul Services can be tested using Rest Assured, an open source library from Java.

GET,POST,PUT,DELETE,PATCH etc requests can be sent to test both JSON and XML web services with it.


Generally, a JSON object is created and the GET,PUT,POST,DELETE requests are sent and the response is validated using JSON path or JSON Object. Below I created a JSON object for POST request(hard coded the values)


import org.json.simple.JSONObject and add the rest assured and json dependencies in pom.xml.



JSON Objects contain key value pairs, in an unordered collection. The same JSON payload can be sent by creating a HashMap in Rest Assured.

A HashMap is a collection from java that uses <key, value> pair, where one acts as a index while other as the value. It always has a unique <key,value> pair and implements the map interface.


import java.util.HashMap for the below code to be executed.





I am passing the values to the hash map using an excel sheet using @Data Provider from TestNG


The code below analyzes the data type(int,string or boolean) of values the excel sheet has using getCellType().Accordingly, the values are put in the hash map and request is sent.




This is the output

In the above example only string values were passed through excel/hashmap/post request.. Below integer, string and boolean value were passed through the excel code and output was got successfully.



This way data driven testing can be achieved through Hashmap in RestAssured.






80 views0 comments