[英]Rest Assured java.util.HashMap cannot be cast to class java.util.List
我正在研究 Rest 保证框架以自动化 API 测试。 实际上,我想将响应作为列表而不是 object。 因为我想为每个元素做断言。 检查每个元素的数据完整性。 我正在执行这段代码:
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);
这是类别 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 原始:
{ “id”:2,“parent_id”:1,“name”:“默认类别”,“is_active”:true,“position”:1,“level”:1,“product_count”:2046,“children_data”: [ {“id”:38,“parent_id”:2,“name”:“最新消息”,“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”:“夹克”,“is_active”:true,“position”:1,“level”:4,“product_count”:186, “children_data”:[] },{“id”:24,“parent_id”:21,“name”:“帽衫和运动衫”,“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,“名称”:“胸罩和坦克”,“is_active”:true,“位置”:4,“级别”: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”:“短裤”,“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”:“夹克”,“is_active”:true,“position”:1,“level”:4,“product_count”:176 , "children_data": [] }, { "id": 15, "parent_id": 12, "name": "帽衫和运动衫 ", "is_active": true, "position": 2, "level": 4, "product_count": 208, "children_data": [] }, { "id": 16, "parent_id": 12, "name" :“三通”,“is_active”:真,“位置”:3,“级别”:4,“product_count”:192,“children_data”:[] },{“id”:17,“parent_id”:12, “名称”:“坦克”,“is_active”:true,“位置”:4,“级别”: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”:“短裤”,“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, "leve l": 3, "product_count": 14, "children_data": [] }, { "id": 5, "parent_id": 3, "name": "健身器材", "is_active": true, "position" :2,“级别”:3,“product_count”:23,“children_data”:[]},{“id”:6,“parent_id”:3,“name”:“手表”,“is_active”:true, “位置”:3,“级别”:3,“product_count”:9,“children_data”:[]}]},{“id”:9,“parent_id”:2,“名称”:“培训”,“ is_active": true, "position": 5, "level": 2, "product_count": 6, "children_data": [ { "id": 10, "parent_id": 9, "name": "视频下载", "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, "lev el": 3, "product_count": 279, "children_data": [] }, { "id": 35, "parent_id": 7, "name": "Performance Fabrics", "is_active": true, "position" :3,“级别”:3,“product_count”:310,“children_data”:[] },{“id”:36,“parent_id”:7,“name”:“环保”,“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,“名称”:“促销”,“is_active”:false,“位置”:6,“级别”:2,“产品计数”:654,“children_data”:[{“id”:30,“parent_id”: 29、“name”:“女装销售”、“is_active”:true、“position”:1、“level”:3、“product_count”:224、“children_data”:[]}、{“id”:31、 "parent_id": 29, "name": "Men Sale", "is_active": true, “位置”:2,“级别”: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" :“销售”,“is_active”:true,“位置”:6,“级别”:2,“product_count”:0,“children_data”:[] } ] }
您想使用getList()
方法获取List<>
,但您的 JSON 不以 List 开头。 而不是getList
使用getObject
,它会工作。 getObject()
将 JSON 映射到您选择的 POJO class。
在反序列化 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
class 字段public
或创建设置器和获取器。 否则,会有例外。
在您的类别中 class;
更改private ArrayList<Category> children_data = null;
到private List<Category> children_data = null;
并尝试执行;
.getList("children_data", Category.class);
这应该有效。
您可以使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.