【发布时间】: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