
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Transform Response to Java List in REST Assured
We can transform the response to Java list in Rest Assured. This can be achieved when we have a JSON array Response. To convert the JSON array to List, we need to use the method as.(List.class).
Once the JSON array Response is converted to a List, we need to convert it to a Map and get all values in the Response in a key-value pair. We shall first send a GET request via Postman on a mock API URL and go through the JSON Response array.
Example
Code Implementation
import java.util.List; import org.testng.annotations.Test; import io.restassured.RestAssured; public class NewTest { @Test public void convertResponsetoList() { //base URL RestAssured.baseURI = "https://run.mocky.io/v3"; //convert JSON Response array to List List<Object> l = RestAssured //GET request on Mock URL .get("/1bb42856-4583-4c18-91ed-b9a6ab19efb4") .as(List.class); //size of List int s = l.size(); System.out.println("List size is: " + s); } }
Output
Advertisements