嵌套Json中的java数据表
希望得到您的帮助和帮助我正在尝试通过cucumber数据表发送嵌套的JSON,但它没有按预期发送,我也尝试了场景大纲,没有解决问题,请帮助我解决,谢谢提前
我有下面的场景
Scenario: provider edits new productWorkingDate
Given productWorkingDates is edited with following fields
| id | productId | fromDate | toDate | name | strictHours | maxUsedTicketsQuantity | errorCode |
| bpvjPBpJ | WaNX2QOd | 2022-07-01 | 2022-12-01 | Test55 | false | 0 | 0 |
And TimeSlots is edited with following fields
| dayOfWeek | startTime | endTime | duration | quantity | usedQuantity | active |
| Sunday | 14:00:00 | 15:00:00 | 02:00:00 | 0 | 0 | true |
| Monday | 14:00:00 | 15:00:00 | 02:00:00 | 0 | 0 | true |
Then verify status code is 200
我有下面的步骤定义
@And("^TimeSlots is edited with following fields$")
public void timeslotsIsCreatedWithFollowingFields(List<Map<String, String>> expectedTimeSlots) {
TimeSlots timeSlots = new TimeSlots();
for(int i = 0; i < expectedTimeSlots.size(); i ++) {
timeSlots.setDayOfWeek(expectedTimeSlots.get(i).get("dayOfWeek"));
timeSlots.setStartTime(expectedTimeSlots.get(i).get("startTime"));
timeSlots.setEndTime((expectedTimeSlots.get(i).get("endTime")));
timeSlots.setDuration(expectedTimeSlots.get(i).get("duration"));
timeSlots.setQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("quantity")));
timeSlots.setUsedQuantity(Integer.parseInt(expectedTimeSlots.get(i).get("usedQuantity")));
timeSlots.setActive(Boolean.parseBoolean(expectedTimeSlots.get(i).get("active")));
}
实际产出为:
{
"productWorkingDate": {
"id": "bpvjPBpJ",
"productId": "WaNX2QOd",
"fromDate": "2022-07-01",
"toDate": "2022-12-01",
"name": "Test55",
"strictHours": false,
"timeSlots": [
{
"id": "Wlqb8XOb",
"productWorkingDateId": "bpvjPBpJ",
"dayOfWeek": "Monday",
"startTime": "14:00:00",
"endTime": "15:00:00",
"duration": "02:00:00",
"quantity": 0,
"usedQuantity": 0,
"active": true,
"deletedAt": null
}
],
"deletedAt": null,
"maxUsedTicketsQuantity": 0,
"errorCode": 0
},
"maxUsedTicketsQuantity": 0,
"error": null,
"errorCode": 0
}
预期产出为:
{
"productWorkingDate": {
"id": "bpvjPBpJ",
"productId": "WaNX2QOd",
"fromDate": "2022-07-01",
"toDate": "2022-12-01",
"name": "Test55",
"strictHours": false,
"timeSlots": [
{
"id": "4lrn8old",
"productWorkingDateId": "bpvjPBpJ",
"dayOfWeek": "Sunday",
"startTime": "14:00:00",
"endTime": "15:00:00",
"duration": "02:00:00",
"quantity": 0,
"usedQuantity": 0,
"active": true,
"deletedAt": null
},
{
"id": "dOnz85OV",
"productWorkingDateId": "bpvjPBpJ",
"dayOfWeek": "Monday",
"startTime": "14:00:00",
"endTime": "15:00:00",
"duration": "02:00:00",
"quantity": 0,
"usedQuantity": 0,
"active": true,
"deletedAt": null
}
],
"deletedAt": null,
"maxUsedTicketsQuantity": 0,
"errorCode": 0
},
"maxUsedTicketsQuantity": 0,
"error": null,
"errorCode": 0
}
时隙的POJO类
我在我的POJO课程中使用龙目图书馆
import lombok.Data;
@Data
public class TimeSlots {
private String id;
private String productWorkingDateId;
private String startTime;
private String endTime;
private String duration;
private Integer quantity;
private Integer usedQuantity;
private boolean active;
private String deletedAt;
private String dayOfWeek;
# 1 楼答案
问题就在这里
在json中
timeSlots
是一个数组,但在方法timeslotsIsCreatedWithFollowingFields
中,您只创建了一个对象TimeSlots timeSlots = new TimeSlots();
,然后通过setter编辑数据。逐步调试:更新: 我不知道,但一般来说,您需要创建一个列表来转换为Json数组
你需要这样的东西
# 2 楼答案
您似乎已经编辑了我复制错误所需的大部分/部分信息,我只是根据您提供的内容构建了一个样本,并成功地获得了所需的输出
您正在为for循环之外的时隙创建一个对象,但它应该在循环内
功能文件:
步骤定义: