如何从功能文件中准备json文件?

问题描述 投票:0回答:2

有没有办法从BDD中的.feature文件准备json文件?

我正在尝试创建json文件,其中输入数据源是.feature文件

Feature: Testing a REST API 
       Scenario: Create student account using post method
       Given api is up and running for post method
       When i create json with below valuesand hit rest api

 | Student_id            |Name       |   CityName    | State  |PostCode    |Tel            |
 |      0101             |Andrew     |  Leeds        |        | SO143FT    | 345345345345  |
 |      0102             |Smith      |  NewCastle    |        | SO143LN    | 345345345345  |
       Then Status is 201

下面是示例json文件。

      {
            "Student_id": 0101,
            "Name": "test",
            "CityName": "test",
            "State": "TT",
            "PostCode": 89098,
            "Tel": "(000)- 000-0000",

        }
json rest cucumber bdd
2个回答
1
投票

找到我的问题的解决方案:table是黄瓜的数据表。

 List<String> jsons = table.asMaps(String.class, String.class)

.stream()
.map(gson::toJson)
.collect(Collectors.toList());

0
投票

创建一个具有所需字段的类Student(如在示例表中),您可以使用像jackson这样的框架从该类创建json。

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
@JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Student {
        int student_id;
        String name;
        String cityName;
        String state;
        int PostCode; //Note: your example has an int, but might be a String actually?
        String Tel;
}

public Student(int student_id, String name, String cityName, String     state, int PostCode, String Tel) {
    this.student_id = student_id;
    this.name = name;
    this.cityName = cityName;
    this.state = state;
    this.PostCode = PostCode;
    this.Tel = PostCode;
}

您需要更新Scenario Outline以获取Examples-table中的值。例如,在Cucumber中,您可以执行以下操作:

When I create a student with <Student_id> and <Name> in <CityName> in <State> with <PostCode> and <Tel>

标有<>的变量将替换为表中的值。

然后,您将实现StepDefinition,您可以使用这些值创建新的Student。我在Student类中添加了一个Constructor。

然后,您需要创建http调用以将创建的学生作为Json发送。

要发布http调用,您可以使用RestAssured之类的框架。 Afaik RestAssured不接受对象,因此您必须从对象生成json。

这是杰克逊如何做到这一点的example

ObjectMapper mapper = new ObjectMapper();学生=新学生(student_id,姓名,cityName,州,PostCode,电话);

// String中对象到JSON字符串jsonInString = mapper.writeValueAsString(user);

然后在你的http调用中使用jsonInString

© www.soinside.com 2019 - 2024. All rights reserved.