【问题标题】:Rest Assured java.util.HashMap cannot be cast to class java.util.List放心 java.util.HashMap 不能转换为 java.util.List 类
【发布时间】:2020-06-09 23:50:37
【问题描述】:

我正在开发放心的框架来自动化 API 测试。实际上我想将响应作为列表而不是作为对象。因为我想为每个元素做断言。检查每个元素的数据完整性。我正在执行这段代码:

    List<Category> categories = given().
            headers(
                    "Authorization",
                    "Bearer key",
                    "Content-Type",
                    ContentType.JSON,
                    "Accept",
                    ContentType.JSON)
            .when()
            .get("/rest/V1/categories").then().extract().response().jsonPath().getList("", Category.class);

这是类别类:

public class Category  {

private Long id = null;

private Long parent_id = null;

private String name = null;

private boolean is_active = true;

private int position = 0;

private int level = 0;

private int product_count = 0;

private ArrayList<Category> children_data = null;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}

public Long getParent_id() {
    return parent_id;
}
public void setParent_id(Long parent_id) {
    this.parent_id = parent_id;
}

/**
 **/
public Boolean getIs_active() {
    return is_active;
}
public void setIs_active(Boolean is_active) {
    this.is_active = is_active;
}

/**
 **/
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

/**
 **/
public int getProduct_count() {
    return product_count;
}
public void setProduct_count(int product_count) {
    this.product_count = product_count;
}

public int getLevel() {
    return level;
}
public void setLevel(int level) {
    this.level = level;
}

public int getPosition() {
    return position;
}
public void setPosition(int position) {
    this.position = position;
}

/**
 **/
public List<Category> getChildren_data() {
    return children_data;
}
public void setChildren_data(List<Category> children_data) {
    this.children_data = children_data;
}

这是控制台日志:

java.lang.ClassCastException: class java.util.HashMap cannot be cast to class java.util.List (java.util.HashMap and java.util.List are in module java.base of loader 'bootstrap')

我的原始 Json:

{ “身份证”:2, “父ID”:1, "name": "默认类别", “is_active”:是的, “位置”:1, “1级, “product_count”:2046, “儿童数据”:[ { “身份证”:38, “父ID”:2, "name": "什么是新的", “is_active”:是的, “位置”:1, “级别”:2, "product_count": 0, “儿童数据”:[] }, { “身份证”:20, “父ID”:2, “名称”:“女性”, “is_active”:是的, “位置”:2, “级别”:2, “product_count”:1012, “儿童数据”:[ { “身份证”:21, “父母ID”:20, “名称”:“上衣”, “is_active”:是的, “位置”:1, “级别”:3, “product_count”:784, “儿童数据”:[ { “身份证”:23, “父ID”:21, “名称”:“夹克”, “is_active”:是的, “位置”:1, “级别”:4, “产品计数”:186, “儿童数据”:[] }, { “身份证”:24, “父ID”:21, “名称”:“连帽衫和运动衫”, “is_active”:是的, “位置”:2, “级别”:4, “产品计数”:182, “儿童数据”:[] }, { “身份证”:25, “父ID”:21, “名称”:“三通”, “is_active”:是的, “位置”:3, “级别”:4, “product_count”:192, “儿童数据”:[] }, { “身份证”:26, “父ID”:21, “名称”:“胸罩和坦克”, “is_active”:是的, “位置”:4, “级别”:4, “产品计数”:224, “儿童数据”:[] } ] }, { “身份证”:22, “父母ID”:20, “名称”:“底部”, “is_active”:是的, “位置”:2, “级别”:3, “产品计数”:228, “儿童数据”:[ { “身份证”:27, “父ID”:22, “名称”:“裤子”, “is_active”:是的, “位置”:1, “级别”:4, “product_count”:91, “儿童数据”:[] }, { “身份证”:28, “父ID”:22, “名称”:“短裤”, “is_active”:是的, “位置”:2, “级别”:4, “产品计数”:137, “儿童数据”:[] } ] } ] }, { “身份证”:11, “父ID”:2, “名称”:“男性”, “is_active”:是的, “位置”:3, “级别”:2, “product_count”:982, “儿童数据”:[ { “身份证”:12, “父ID”:11, “名称”:“上衣”, “is_active”:是的, “位置”:1, “级别”:3, “产品计数”:678, “儿童数据”:[ { “身份证”:14, “父ID”:12, “名称”:“夹克”, “is_active”:是的, “位置”:1, “级别”:4, “产品计数”:176, “儿童数据”:[] }, { “身份证”:15, “父ID”:12, “名称”:“连帽衫和运动衫”, “is_active”:是的, “位置”:2, “级别”:4, “product_count”:208, “儿童数据”:[] }, { “身份证”:16, “父ID”:12, “名称”:“三通”, “is_active”:是的, “位置”:3, “级别”:4, “product_count”:192, “儿童数据”:[] }, { “身份证”:17, “父ID”:12, “名称”:“坦克”, “is_active”:是的, “位置”:4, “级别”:4, “product_count”:102, “儿童数据”:[] } ] }, { “身份证”:13, “父ID”:11, “名称”:“底部”, “is_active”:是的, “位置”:2, “级别”:3, “product_count”:304, “儿童数据”:[ { “身份证”:18, “父ID”:13, “名称”:“裤子”, “is_active”:是的, “位置”:1, “级别”:4, “产品计数”:156, “儿童数据”:[] }, { “身份证”:19, “父ID”:13, “名称”:“短裤”, “is_active”:是的, “位置”:2, “级别”:4, “产品计数”:148, “儿童数据”:[] } ] } ] }, { “身份证”:3, “父ID”:2, “名称”:“齿轮”, “is_active”:是的, “位置”:4, “级别”:2, “product_count”:46, “儿童数据”:[ { “身份证”:4, “父ID”:3, “名称”:“袋子”, “is_active”:是的, “位置”:1, “级别”:3, “product_count”:14, “儿童数据”:[] }, { “身份证”:5, “父ID”:3, "name": "健身器材", “is_active”:是的, “位置”:2, “级别”:3, “产品计数”:23, “儿童数据”:[] }, { “身份证”:6, “父ID”:3, “名称”:“手表”, “is_active”:是的, “位置”:3, “级别”:3, “product_count”:9, “儿童数据”:[] } ] }, { “身份证”:9, “父ID”:2, “名称”:“培训”, “is_active”:是的, “位置”:5, “级别”:2, “product_count”:6, “儿童数据”:[ { “身份证”:10, “父ID”:9, "name": "视频下载", “is_active”:是的, “位置”:1, “级别”:3, “product_count”:6, “儿童数据”:[] } ] }, { “身份证”:7, “父ID”:2, “名称”:“收藏”, “is_active”:假, “位置”:5, “级别”:2, “product_count”:989, “儿童数据”:[ { “身份证”:8, “父ID”:7, "name": "新 Luma Yoga 系列", “is_active”:是的, “位置”:1, “级别”:3, “产品计数”:347, “儿童数据”:[] }, { “身份证”:34, “父ID”:7, "name": "Erin 推荐", “is_active”:是的, “位置”:2, “级别”:3, “产品计数”:279, “儿童数据”:[] }, { “身份证”:35, “父ID”:7, "name": "高性能面料", “is_active”:是的, “位置”:3, “级别”:3, “product_count”:310, “儿童数据”:[] }, { “身份证”:36, “父ID”:7, “名称”:“环保”, “is_active”:是的, “位置”:4, “级别”:3, “产品计数”:247, “儿童数据”:[] }, { “身份证”:39, “父ID”:7, "name": "性能运动服新款", “is_active”:是的, “位置”:5, “级别”:3, "product_count": 0, “儿童数据”:[] }, { “身份证”:40, “父ID”:7, "name": "生态系列新品", “is_active”:是的, “位置”:6, “级别”:3, "product_count": 0, “儿童数据”:[] } ] }, { “身份证”:29, “父ID”:2, "name": "促销活动", “is_active”:假, “位置”:6, “级别”:2, “product_count”:654, “儿童数据”:[ { “身份证”:30, “父母ID”:29, "name": "女装销售", “is_active”:是的, “位置”:1, “级别”:3, “产品计数”:224, “儿童数据”:[] }, { “身份证”:31, “父母ID”:29, "name": "男士销售", “is_active”:是的, “位置”:2, “级别”:3, “product_count”:39, “儿童数据”:[] }, { “身份证”:32, “父母ID”:29, “名称”:“裤子”, “is_active”:是的, “位置”:3, “级别”:3, “产品计数”:247, “儿童数据”:[] }, { “身份证”:33, “父母ID”:29, “名称”:“三通”, “is_active”:是的, “位置”:4, “级别”:3, “product_count”:192, “儿童数据”:[] } ] }, { “身份证”:37, “父ID”:2, “名称”:“销售”, “is_active”:是的, “位置”:6, “级别”:2, "product_count": 0, “儿童数据”:[] } ] }

【问题讨论】:

  • 您能否了解原始 JSON 的外观并将其添加到问题中?
  • @chef417 Json raw 已添加,谢谢
  • 您的 JSON 似乎无效 - 我尝试将其复制到在线 JSON 编辑器并报告无效,例如在这里试试:jsoneditoronline.org/#left=local.qaqola&right=local.qecuzu .. 乍一看,你似乎有 6 个开方括号 [ 和 3 个闭方括号 ],这告诉我问题可能是无效的 JSON
  • @chef417 提供的 json 格式正确。
  • @user3669148 - 尝试在类别类中添加private Category[] children_data;。并更改 getter 和 setter。此外,尝试将 getList() 方法更改为 .getList("$", Category.class); 您将获得一个包含单个项目的 List。并且所有其他children_data 将在该项目的(类别类型)数组中。如果您想跳过初始对象 ("name": "Default Category") 并将所有其他 children_data 获取到 List,则将 getList() 方法更改为 .getList("children_data", Category.class);

标签: java automation rest-assured web-api-testing rest-assured-jsonpath


【解决方案1】:

您想使用getList() 方法获得List&lt;&gt;,但您的JSON 不是以List 开头。而不是getList 使用getObject 它会工作。 getObject() 将 JSON 映射到您选择的 POJO 类。

在反序列化 JSON 时,您可能会遇到以下问题

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of dataentities.Category (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

您需要做的就是添加一个依赖项来告诉 Rest-Assured 您要使用哪个反序列化器。 我个人的选择是:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9</version>
</dependency>

编辑: 要将 JSON 反序列化为 POJO,您可以使用 getObject() 方法,如下所示:

Category category = jsonPath.getObject("", Category.class);

编辑: 你应该让你的Category 类字段public 或者创建setter 和getter。否则会有例外。

【讨论】:

  • 嘿 Fenio,一直在关注这个问题并为答案 +1,但我们如何获得 children_data 的值? categories.getId() 工作并返回 2 但我们如何获得 ID 38 ?
  • @WilfredClement 最简单的解决方案是category.children_data.get(0).id
  • @WilfredClement 我想您想知道使用特定方法的解决方案吗?
  • 我很乐意看看有没有,我暂时用categories.getChildren_data().get(0).getId()
  • @Fenio 我收到此错误:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造dataentities.Category 的实例(没有创建者,如默认构造,存在):无法从对象值反序列化(没有基于委托或基于属性的创建者)在 [Source: (String)"
【解决方案2】:

在您的类别类中;

更改private ArrayList&lt;Category&gt; children_data = null;

private List&lt;Category&gt; children_data = null;

并尝试执行;

.getList("children_data", Category.class);

这应该可行。

【讨论】:

    【解决方案3】:

    您可以使用 Gson 库将您的 json 响应转换为 POJO

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
    

    然后使用此语句获取预期的类别数组。

    Response response = given()
            .headers("Authorization","Bearer key",
                     "Content-Type",ContentType.JSON,
                     "Accept",ContentType.JSON)
            .get("/rest/V1/categories");
    
    Category[] categories = new Gson().fromJson(response.getBody().asString(),Category[].class);
    

    现在断言类别,否则您可能需要一个列表,然后执行此操作,

    List<Category> categoryList = Arrays.asList(categories);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      相关资源
      最近更新 更多