如何从功能文件准备json文件?
有没有办法从BDD中的.feature文件准备json文件 我正在尝试创建json文件,其中数据的输入源是.feature文件如何从功能文件准备json文件?,json,rest,cucumber,bdd,Json,Rest,Cucumber,Bdd,有没有办法从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 h
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",
}
创建一个包含所需字段的类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;
}
您需要更新场景大纲,以获取示例表中的值。例如,在Cucumber中,您可以执行以下操作:
当我创建一个带有和的学生时
标记为的变量将替换为表中的值
然后,您将实现一个StepDefinition,在其中使用这些值创建一个新学生。我已经向学生类添加了一个构造函数
然后需要创建http调用,以Json的形式发送创建的学生
要发布http调用,可以使用restasured之类的框架。
Afaik RestAssured不接受对象,因此您必须从对象生成json
下面是一个关于如何与杰克逊一起做到这一点的例子
ObjectMapper mapper=新的ObjectMapper();
学生=新学生(学生id、姓名、城市名称、州、邮政编码、电话)
//对象转换为字符串形式的JSON
字符串jsonInString=mapper.writeValueAsString(用户)
然后在http调用中使用
jsonInString
。找到了我的问题的解决方案:cucumber中的table是datatable
List<String> jsons = table.asMaps(String.class, String.class)
.stream()
.map(gson::toJson)
.collect(Collectors.toList());
List jsons=table.asMaps(String.class,String.class)
.stream()
.map(gson::toJson)
.collect(Collectors.toList());
谢谢您的帮助。你能给我提供示例代码吗?我已经更新了答案。这对你来说足够了,还是你需要更多?(如果是,请具体说明)是的,如果您能为我提供完整的解决方案,那就太好了。我使用的是宁静,请放心。我一直在努力解决这个问题,但仍然面临这个问题。@Dipak我无法为您提供一个完整的解决方案,因为我对Serenity的经验有限。我已经扩展了上面的答案;让我知道你自己能做多少,如果你有进一步的问题,发布更新。谢谢你宝贵的回复,但你不认为如果你有10列或更多列的表,在这种情况下,功能文件看起来会很难看吗。