簡體   English   中英

黃瓜中的數據表發送嵌套的 Json

[英]Data table in cucumber sending nested Json

尋求您的幫助和幫助我正在嘗試通過黃瓜數據表發送嵌套的 JSON,但它沒有按預期發送,我也嘗試了 Scenario Outline,但沒有解決問題,請幫我解決在此先感謝

我有以下場景;

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
}

TimeSlots 的 POJO 類

我在我的 POJO 類 lombok 庫中使用;

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;

問題在這里

json 中的timeSlots是一個數組,但在方法timeslotsIsCreatedWithFollowingFields您只創建了 1 個對象TimeSlots timeSlots = new TimeSlots(); 然后通過setter編輯數據。 一步步調試:

TimeSlots timeSlots = new TimeSlots();
--------
i = 0; setA(0) ---> A = 0
--------
i = 1; setA(1) ---> A = 1
--------
end: timeSlots(A=1)

更新:我不知道黃瓜,但一般來說,您需要創建一個 List 來轉換為 Json 數組。

你需要這樣的東西

@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);
    }
}

您似乎已經編輯了我復制錯誤所需的大部分/部分信息,我只是根據您提供的內容構建了一個示例,並設法獲得了所需的輸出

您正在 for 循環外為 TimeSlots 創建一個對象,但它應該在循環內

功能文件:

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 |

步驟定義:

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")...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM