How to prepare json file from feature file?

若如初见. 提交于 2019-12-13 03:23:13

问题


Is there any way to prepare json file from .feature file in BDD?

I am trying to create json file where input source of data is .feature file

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.

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

        }

回答1:


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());



回答2:


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.

@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. 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.

To post the http call you can use a framework like RestAssured. Afaik RestAssured does not take objects, so you'lL have to generate the json from the object.

Here's an example of how to do that with jackson.

ObjectMapper mapper = new ObjectMapper(); Student student = new Student(student_id, name, cityName, state, PostCode, Tel);

//Object to JSON in String String jsonInString = mapper.writeValueAsString(user);

Then use jsonInString in your http call.



来源:https://stackoverflow.com/questions/48487298/how-to-prepare-json-file-from-feature-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!