How to prepare json file from feature file?

2019-08-31 02:43发布

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",

        }

2条回答
戒情不戒烟
2楼-- · 2019-08-31 03:05

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.

查看更多
做自己的国王
3楼-- · 2019-08-31 03:12

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());
查看更多
登录 后发表回答