如何在 Rest Assured 中将响应转换为 Java 列表?

rest assureddynamic programmingprogramming

我们可以在 Rest Assured 中将响应转换为 Java 列表。这可以在我们拥有 JSON 数组 Response 时实现。要将 JSON 数组转换为 List,我们需要使用 as.(List.class) 方法。

将 JSON 数组 Response 转换为 List 后,我​​们需要将其转换为 Map,并以键值对的形式获取 Response 中的所有值。我们首先通过 Postman 向模拟 API URL 发送 GET 请求,并遍历 JSON Response 数组。

示例

代码实现

import java.util.List;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
public class NewTest {
   @Test
   public void convertResponsetoList() {

      //基础 URL
      RestAssured.baseURI = "https://run.mocky.io/v3";

      //将 JSON 响应数组转换为 List
      List<Object> l = RestAssured

      //对模拟 URL 发起 GET 请求
      .get("/1bb42856-4583-4c18-91ed-b9a6ab19efb4")
      .as(List.class);

      //列表的大小
      int s = l.size();
      System.out.println("List size is: " + s);
   }
}

输出


相关文章