简体   繁体   English

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

[英]How to prepare json file from feature file?

Is there any way to prepare json file from .feature file in BDD? 有什么办法可以从BDD中的.feature文件准备json文件吗?

I am trying to create json file where input source of data is .feature file 我正在尝试创建数据输入源为.feature文件的json文件

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

Below is the sample json file. 以下是示例json文件。

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

        }

Found solution for my problem: table is datatable in cucumber. 找到解决我的问题的方法:黄瓜中的表是数据表。

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

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

Create a class Student with the desired fields (as in your Example table) and you can use a framework like jackson to create json from that class. 创建一个具有所需字段的学生类(如您的示例表中所示),然后可以使用诸如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;
}

You'll need to update the Scenario Outline to take the values from the Examples-table. 您需要更新方案大纲,以从“示例”表中获取值。 For instance, in Cucumber you could do the following: 例如,在黄瓜中,您可以执行以下操作:

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

The variables marked with <> will be replaced with the values from the table. 标记为<>的变量将替换为表中的值。

You would then implement a StepDefinition where you create a new Student with these values. 然后,您将实现StepDefinition,在其中使用这些值创建一个新的Student。 I've added a Constructor to the Student class. 我已经在“学生”类中添加了一个构造函数。

You'll then need to create the http call to send the created student as Json. 然后,您需要创建http调用,以将创建的学生发送为Json。

To post the http call you can use a framework like RestAssured. 要发布http调用,您可以使用RestAssured之类的框架。 Afaik RestAssured does not take objects, so you'lL have to generate the json from the object. Afaik RestAssured不接受对象,因此您必须从对象生成json。

Here's an example of how to do that with jackson. 这是一个如何与杰克逊一起做的例子

ObjectMapper mapper = new ObjectMapper(); ObjectMapper映射器=新的ObjectMapper(); Student student = new Student(student_id, name, cityName, state, PostCode, Tel); 学生人数=新学生(student_id,姓名,城市名称,州,邮政编码,电话);

//Object to JSON in String String jsonInString = mapper.writeValueAsString(user); //字符串中的JSON对象jsonInString = mapper.writeValueAsString(user);

Then use jsonInString in your http call. 然后在您的http调用中使用jsonInString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM