在this question的基础上,提供的示例似乎将特征文件中的文本锁定为Java编程风格过多(请注意,该文本以大写形式编写,并且只有一个单词。
当功能文件中包含更多“人类可读”的文本时,是否可以传递枚举?例如。:
简单的例子
Feature: Setup Enum and Print value
In order to manage my Enum
As a System Admin
I want to get the Enum
Scenario Outline: Verify Enum Print
When I supply a more human readable text to be converted to <Enum>
Examples: Text can have multiple formats
|Enum |
|Christmas |
|New Year Eve |
|independence-day|
我认为枚举可能类似于:
public enum Holiday {
CHRISTMAS("Christmas"),NEW_YEAR("New Year"),INDEPENDENCE_DAY("independence-day");
private String extendedName;
private Holidays(String extendedName) {
this.extendedName = extendedName;
}
}
我们如何将一个转换为另一个?
更复杂的例子
在更复杂的示例中,我们将其传递给ScenarioObject
Scenario: Enum within a Scenario Object
When I supply a more human readable text to be converted in the objects:
|Holiday |Character|
|Christmas |Santa |
|New Year Eve |Harry|
|independence-day|John Adams|
public class ScenarioObject{
private String character;
private Holiday holiday;
(...getters and setters)
}
更新:
如果唯一的解决方案是应用
Transformer
,as described here,将不胜枚举如何将其应用于ScenarioObject
的示例,因为仅用@XStreamConverter(HolidayTransformer.class)
标记枚举不足以实现转换器。在ScenarioObject
中工作。 最佳答案
到目前为止,我为此找到的最佳解决方案是使用变压器。
对于带有ScenarioObject
的复杂示例,这涉及:
用转换器标记枚举
@XStreamConverter(HolidayTransformer.class)
public enum Holiday {
CHRISTMAS("Christmas"),NEW_YEAR("New Year"),INDEPENDENCE_DAY("independence-day");
private String extendedName;
private Holidays(String extendedName) {
this.extendedName = extendedName;
}
public static Holiday fromString(String type) throws Exception {...}
}
创建变压器
public class HolidayTransformer extends Transformer<Holiday> {
@Override
public Holiday transform(String value) {
try {
return Holiday.fromString(value);
} catch (Exception e) {
fail("Could not convert from value");
return null;
}
}
}
同时用转换器标记ScenarioObject
public class ScenarioObject{
private String character;
@XStreamConverter(HolidayTransformer.class)
private Holiday holiday;
(...getters and setters)
}