放心的 java.util.HashMap 不能转换为类 java.util.List [英] Rest Assured java.util.HashMap cannot be cast to class java.util.List

查看:59
本文介绍了放心的 java.util.HashMap 不能转换为类 java.util.List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用于自动化 API 测试的放心框架.实际上,我想将响应作为列表而不是对象.因为我想为每个元素做断言.检查每个元素的数据完整性.我正在执行此代码:

I am working on Rest assured framework to automate API testing. actually I want to get the response as list not as an object. cause I want to do assertions for each element. to check data integrity for each element. I am executing this code:

    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 原始数据:

My Json raw:

{身份证":2,parent_id":1,"name": "默认类别",is_active":真,位置":1,1级,product_count":2046,儿童数据":[{身份证":38,parent_id":2,"name": "最新消息",is_active":真,位置":1,级别":2,"product_count": 0,children_data":[]},{身份证":20,parent_id":2,"name": "女性",is_active":真,位置":2,级别":2,product_count":1012,儿童数据":[{身份证":21,parent_id":20,"name": "上衣",is_active":真,位置":1,级别":3,产品计数":784,儿童数据":[{身份证":23,parent_id":21,"name": "夹克",is_active":真,位置":1,级别":4,product_count":186,儿童数据":[]},{身份证":24,parent_id":21,"name": "连帽衫和运动衫",is_active":真,位置":2,级别":4,product_count":182,儿童数据":[]},{身份证":25,parent_id":21,"name": "T恤",is_active":真,位置":3,级别":4,product_count":192,儿童数据":[]},{身份证":26,parent_id":21,"name": "胸罩和背心",is_active":真,位置":4,级别":4,product_count":224,儿童数据":[]}]},{身份证":22,parent_id":20,"name": "底部",is_active":真,位置":2,级别":3,product_count":228,儿童数据":[{身份证":27,parent_id":22,"name": "裤子",is_active":真,位置":1,级别":4,"product_count": 91,儿童数据":[]},{身份证":28,parent_id":22,"name": "短裤",is_active":真,位置":2,级别":4,产品计数":137,儿童数据":[]}]}]},{身份证":11,parent_id":2,"name": "男人",is_active":真,位置":3,级别":2,product_count":982,儿童数据":[{身份证":12,parent_id":11,"name": "上衣",is_active":真,位置":1,级别":3,product_count":678,儿童数据":[{身份证":14,parent_id":12,"name": "夹克",is_active":真,位置":1,级别":4,product_count":176,儿童数据":[]},{身份证":15,parent_id":12,"name": "连帽衫和运动衫",is_active":真,位置":2,级别":4,product_count":208,儿童数据":[]},{身份证":16,parent_id":12,"name": "T恤",is_active":真,位置":3,级别":4,product_count":192,儿童数据":[]},{身份证":17,parent_id":12,"name": "坦克",is_active":真,位置":4,级别":4,"product_count": 102,儿童数据":[]}]},{身份证":13,parent_id":11,"name": "底部",is_active":真,位置":2,级别":3,product_count":304,儿童数据":[{身份证":18,parent_id":13,"name": "裤子",is_active":真,位置":1,级别":4,product_count":156,儿童数据":[]},{身份证":19,parent_id":13,"name": "短裤",is_active":真,位置":2,级别":4,"product_count": 148,儿童数据":[]}]}]},{身份证":3,parent_id":2,"name": "齿轮",is_active":真,位置":4,级别":2,"product_count": 46,儿童数据":[{身份证":4,parent_id":3,"name": "包",is_active":真,位置":1,级别":3,"product_count": 14,儿童数据":[]},{身份证":5,parent_id":3,"name": "健身器材",is_active":真,位置":2,级别":3,"product_count": 23,children_data":[]},{身份证":6,parent_id":3,"name": "手表",is_active":真,位置":3,级别":3,"product_count": 9,儿童数据":[]}]},{身份证":9,parent_id":2,"name": "培训",is_active":真,位置":5,级别":2,"product_count": 6,儿童数据":[{身份证":10,parent_id":9,"name": "视频下载",is_active":真,位置":1,级别":3,"product_count": 6,儿童数据":[]}]},{身份证":7,parent_id":2,"name": "集合",is_active":假,位置":5,级别":2,product_count":989,儿童数据":[{身份证":8,parent_id":7,"name": "新 Luma 瑜伽系列",is_active":真,位置":1,级别":3,product_count":347,儿童数据":[]},{身份证":34,parent_id":7,"name": "艾琳推荐",is_active":真,位置":2,级别":3,product_count":279,儿童数据":[]},{身份证":35,parent_id":7,"name": "性能面料",is_active":真,位置":3,级别":3,product_count":310,儿童数据":[]},{身份证":36,parent_id":7,"name": "环保",is_active":真,位置":4,级别":3,product_count":247,儿童数据":[]},{身份证":39,parent_id":7,"name": "性能运动装新品",is_active":真,位置":5,级别":3,"product_count": 0,儿童数据":[]},{身份证":40,parent_id":7,"name": "生态系列新品",is_active":真,位置":6,级别":3,"product_count": 0,儿童数据":[]}]},{身份证":29,parent_id":2,"name": "促销活动",is_active":假,位置":6,级别":2,product_count":654,儿童数据":[{身份证":30,parent_id":29,"name": "女性特卖",is_active":真,位置":1,级别":3,product_count":224,children_data":[]},{身份证":31,parent_id":29,"name": "男士销售",is_active":真,位置":2,级别":3,"product_count": 39,儿童数据":[]},{身份证":32,parent_id":29,"name": "裤子",is_active":真,位置":3,级别":3,product_count":247,children_data":[]},{身份证":33,parent_id":29,"name": "T恤",is_active":真,位置":4,级别":3,product_count":192,儿童数据":[]}]},{身份证":37,parent_id":2,"name": "销售",is_active":真,位置":6,级别":2,"product_count": 0,儿童数据":[]}]}

{ "id": 2, "parent_id": 1, "name": "Default Category", "is_active": true, "position": 1, "level": 1, "product_count": 2046, "children_data": [ { "id": 38, "parent_id": 2, "name": "What's New", "is_active": true, "position": 1, "level": 2, "product_count": 0, "children_data": [] }, { "id": 20, "parent_id": 2, "name": "Women", "is_active": true, "position": 2, "level": 2, "product_count": 1012, "children_data": [ { "id": 21, "parent_id": 20, "name": "Tops", "is_active": true, "position": 1, "level": 3, "product_count": 784, "children_data": [ { "id": 23, "parent_id": 21, "name": "Jackets", "is_active": true, "position": 1, "level": 4, "product_count": 186, "children_data": [] }, { "id": 24, "parent_id": 21, "name": "Hoodies & Sweatshirts", "is_active": true, "position": 2, "level": 4, "product_count": 182, "children_data": [] }, { "id": 25, "parent_id": 21, "name": "Tees", "is_active": true, "position": 3, "level": 4, "product_count": 192, "children_data": [] }, { "id": 26, "parent_id": 21, "name": "Bras & Tanks", "is_active": true, "position": 4, "level": 4, "product_count": 224, "children_data": [] } ] }, { "id": 22, "parent_id": 20, "name": "Bottoms", "is_active": true, "position": 2, "level": 3, "product_count": 228, "children_data": [ { "id": 27, "parent_id": 22, "name": "Pants", "is_active": true, "position": 1, "level": 4, "product_count": 91, "children_data": [] }, { "id": 28, "parent_id": 22, "name": "Shorts", "is_active": true, "position": 2, "level": 4, "product_count": 137, "children_data": [] } ] } ] }, { "id": 11, "parent_id": 2, "name": "Men", "is_active": true, "position": 3, "level": 2, "product_count": 982, "children_data": [ { "id": 12, "parent_id": 11, "name": "Tops", "is_active": true, "position": 1, "level": 3, "product_count": 678, "children_data": [ { "id": 14, "parent_id": 12, "name": "Jackets", "is_active": true, "position": 1, "level": 4, "product_count": 176, "children_data": [] }, { "id": 15, "parent_id": 12, "name": "Hoodies & Sweatshirts", "is_active": true, "position": 2, "level": 4, "product_count": 208, "children_data": [] }, { "id": 16, "parent_id": 12, "name": "Tees", "is_active": true, "position": 3, "level": 4, "product_count": 192, "children_data": [] }, { "id": 17, "parent_id": 12, "name": "Tanks", "is_active": true, "position": 4, "level": 4, "product_count": 102, "children_data": [] } ] }, { "id": 13, "parent_id": 11, "name": "Bottoms", "is_active": true, "position": 2, "level": 3, "product_count": 304, "children_data": [ { "id": 18, "parent_id": 13, "name": "Pants", "is_active": true, "position": 1, "level": 4, "product_count": 156, "children_data": [] }, { "id": 19, "parent_id": 13, "name": "Shorts", "is_active": true, "position": 2, "level": 4, "product_count": 148, "children_data": [] } ] } ] }, { "id": 3, "parent_id": 2, "name": "Gear", "is_active": true, "position": 4, "level": 2, "product_count": 46, "children_data": [ { "id": 4, "parent_id": 3, "name": "Bags", "is_active": true, "position": 1, "level": 3, "product_count": 14, "children_data": [] }, { "id": 5, "parent_id": 3, "name": "Fitness Equipment", "is_active": true, "position": 2, "level": 3, "product_count": 23, "children_data": [] }, { "id": 6, "parent_id": 3, "name": "Watches", "is_active": true, "position": 3, "level": 3, "product_count": 9, "children_data": [] } ] }, { "id": 9, "parent_id": 2, "name": "Training", "is_active": true, "position": 5, "level": 2, "product_count": 6, "children_data": [ { "id": 10, "parent_id": 9, "name": "Video Download", "is_active": true, "position": 1, "level": 3, "product_count": 6, "children_data": [] } ] }, { "id": 7, "parent_id": 2, "name": "Collections", "is_active": false, "position": 5, "level": 2, "product_count": 989, "children_data": [ { "id": 8, "parent_id": 7, "name": "New Luma Yoga Collection", "is_active": true, "position": 1, "level": 3, "product_count": 347, "children_data": [] }, { "id": 34, "parent_id": 7, "name": "Erin Recommends", "is_active": true, "position": 2, "level": 3, "product_count": 279, "children_data": [] }, { "id": 35, "parent_id": 7, "name": "Performance Fabrics", "is_active": true, "position": 3, "level": 3, "product_count": 310, "children_data": [] }, { "id": 36, "parent_id": 7, "name": "Eco Friendly", "is_active": true, "position": 4, "level": 3, "product_count": 247, "children_data": [] }, { "id": 39, "parent_id": 7, "name": "Performance Sportswear New", "is_active": true, "position": 5, "level": 3, "product_count": 0, "children_data": [] }, { "id": 40, "parent_id": 7, "name": "Eco Collection New", "is_active": true, "position": 6, "level": 3, "product_count": 0, "children_data": [] } ] }, { "id": 29, "parent_id": 2, "name": "Promotions", "is_active": false, "position": 6, "level": 2, "product_count": 654, "children_data": [ { "id": 30, "parent_id": 29, "name": "Women Sale", "is_active": true, "position": 1, "level": 3, "product_count": 224, "children_data": [] }, { "id": 31, "parent_id": 29, "name": "Men Sale", "is_active": true, "position": 2, "level": 3, "product_count": 39, "children_data": [] }, { "id": 32, "parent_id": 29, "name": "Pants", "is_active": true, "position": 3, "level": 3, "product_count": 247, "children_data": [] }, { "id": 33, "parent_id": 29, "name": "Tees", "is_active": true, "position": 4, "level": 3, "product_count": 192, "children_data": [] } ] }, { "id": 37, "parent_id": 2, "name": "Sale", "is_active": true, "position": 6, "level": 2, "product_count": 0, "children_data": [] } ] }

推荐答案

您想使用 getList() 方法获得 List<> 但您的 JSON 没有从列表开始.而不是 getList 使用 getObject ,它会工作.getObject() 将 JSON 映射到您选择的 POJO 类.

You want to get a List<> using getList() method but your JSON doesn't start with List. Instead of getList use getObject and it will work. getObject() maps the JSON to POJO class of your choice.

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

While deserializing JSON, you might encounter below issue

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 您要使用哪个反序列化器.我个人的选择是:

All you need to do is add a dependency to tell Rest-Assured which deserializer you want to use. My personal choice is:

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

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

To deserialize JSON into a POJO you use getObject() method like this:

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

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

You should make your Category class fields public OR create setters and getters. Otherwise, there will be exceptions.

这篇关于放心的 java.util.HashMap 不能转换为类 java.util.List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆