【发布时间】:2017-07-21 10:28:10
【问题描述】:
我想在我的功能文件中使用数据表,并将内容转换为步骤定义中的单个特定类型。例如,我希望能够将以下数据表转换为 User 类的单个实例。
Given the user is
| firstname | lastname | nationality |
| Roberto | Lo Giacco | Italian |
用户类:
class User {
String firstName;
String lastName;
Nationality nationality;
//getters / setters / etc
}
步骤定义:
@Given("^the user is$")
public void the_user_is(User user) {
this.user = user;
}
但是,当我运行它时,我收到以下错误:
cucumber.runtime.CucumberException: cucumber.runtime.CucumberException:不是 Map 或 List 类型:类 example.User
文档建议可以将数据表转换为单个对象。
但是,通过检查代码,它看起来总是会返回一个列表。这段代码 sn-p 来自 cucumber/runtime/table/TableConverter.convert(DataTable dataTable, Type type, boolean transposed) :
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a Map or List type: " + type);
}
我知道它适用于列表:
@Given("^the user is$")
public void the_user_is(List<User> user) {
this.user = user.get(0);
}
但在我的真实世界步骤中(而不是这个简化的示例),只有一个需要创建的对象,我想避免使用列表然后获取第一个项目。
【问题讨论】:
-
为什么不把数据表传给你的step方法呢?
-
重点是步骤定义中不必有DataTable或List,而是直接给出特征文件中定义的对象的单个实例。