首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >黄瓜发送嵌套Json中的数据表

黄瓜发送嵌套Json中的数据表
EN

Stack Overflow用户
提问于 2021-07-23 12:45:01
回答 2查看 2.3K关注 0票数 0

我试图通过黄瓜数据表发送嵌套的JSON,但没有按预期发送,我也尝试过方案大纲,没有解决问题,请帮助我提前解决它

我有以下情况;

代码语言:javascript
复制
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

我有下面的步骤定义

代码语言:javascript
复制
 @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")));

              }

实际产出如下:

代码语言:javascript
复制
{
    "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
}

预期产出如下:

代码语言:javascript
复制
{
    "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
}

用于 TimeSlots的POJO类

我使用我的POJO类lombok库;

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-27 13:31:07

您似乎已经修改了复制错误所需的大部分/部分信息,我只是根据您所给出的内容构造了一个示例,并成功地获得了所需的输出。

您正在为TimeSlots在for循环之外创建一个对象,但是它应该在循环中。

特性文件:

代码语言:javascript
复制
Feature: STACK

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 | productWorkingDateId | id       |
| Sunday    | 14:00:00  | 15:00:00 | 02:00:00 |        0 |            0 | true   | bpvjPBpJ             | 4lrn8old |
| Monday    | 14:00:00  | 15:00:00 | 02:00:00 |        0 |            0 | true   | bpvjPBpJ             | dOnz85OV |

步骤定义:

代码语言:javascript
复制
ProductWorkingDate pw = new ProductWorkingDate();
Example ex = new Example();

@Given("productWorkingDates is edited with following fields")
public void product_working_dates_is_edited_with_following_fields(io.cucumber.datatable.DataTable dataTable) {

    pw.setId("bpvjPBpJ");
    pw.setProductId("WaNX2QOd");
    pw.setFromDate("2022-07-01");
    pw.setToDate("2022-12-01");
    pw.setName("Test55");
    pw.setStrictHours(false);

}

@Given("TimeSlots is edited with following fields")
public void time_slots_is_edited_with_following_fields(List<Map<String, String>> expectedTimeSlots)
        throws JsonProcessingException {

    pw.setMaxUsedTicketsQuantity(0);
    pw.setDeletedAt("Test");
    pw.setErrorCode(0);

    List<TimeSlots> listTimeSlots = new ArrayList<TimeSlots>();

    for (int i = 0; i < expectedTimeSlots.size(); i++) {

        TimeSlots timeSlots = new TimeSlots();

        timeSlots.setId(expectedTimeSlots.get(i).get("id"));
        timeSlots.setProductWorkingDateId(expectedTimeSlots.get(i).get("productWorkingDateId"));
        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")));

        listTimeSlots.add(timeSlots);

    }

    pw.setTimeSlots(listTimeSlots);
    ex.setProductWorkingDate(pw);
    ex.setMaxUsedTicketsQuantity(0);
    ex.setError("test");
    ex.setErrorCode(0);

    RestAssured.given().body(ex).when().post("http://localhost:8080/stack")...
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-24 00:27:11

问题在这里,

json中的timeSlots是一个数组,但是在方法timeslotsIsCreatedWithFollowingFields中,您只创建了一个对象TimeSlots timeSlots = new TimeSlots();,然后通过setter编辑数据。一步一步调试:

代码语言:javascript
复制
TimeSlots timeSlots = new TimeSlots();
--------
i = 0; setA(0) ---> A = 0
--------
i = 1; setA(1) ---> A = 1
--------
end: timeSlots(A=1)

更新:我不知道黄瓜,但一般来说,您需要创建一个列表来转换为Json数组。

你需要这样的东西

代码语言:javascript
复制
@Given("TimeSlots is edited with following fields")
public void timeslotsIsCreatedWithFollowingFields(List<Map<String, String>> expectedTimeSlots) {
    List<TimeSlots> listTimeSlots = new ArrayList<>();

    for (Map<String, String> expectedTimeSlot : expectedTimeSlots) {
        TimeSlots timeSlots = new TimeSlots();
        timeSlots.setDayOfWeek(expectedTimeSlot.get("dayOfWeek"));
        timeSlots.setStartTime(expectedTimeSlot.get("startTime"));
        timeSlots.setEndTime((expectedTimeSlot.get("endTime")));
        timeSlots.setDuration(expectedTimeSlot.get("duration"));
        timeSlots.setQuantity(Integer.parseInt(expectedTimeSlot.get("quantity")));
        timeSlots.setUsedQuantity(Integer.parseInt(expectedTimeSlot.get("usedQuantity")));
        timeSlots.setActive(Boolean.parseBoolean(expectedTimeSlot.get("active")));
        
        listTimeSlots.add(timeSlots);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68499411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档