是否可以将Java-Enum作为参数从黄瓜特征文件传递(以更加文本友好的方式)?

时间:2018-04-18 11:19:55

标签: java enums cucumber cucumber-jvm

this question上构建,提供的示例似乎将特征文件中的文本锁定为Java编程样式(注意文本是全部大写的,只有一个字。

当特征文件具有更多"人类可读"时,是否可以传递枚举?文本? E.g:

简单示例

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)
}

更新 如果唯一的解决方案是应用Transformeras described here,那么应该感谢一个应用于ScenarioObject的示例,因为只需使用{{1标记枚举变换器在@XStreamConverter(HolidayTransformer.class)内工作是不够的。

1 个答案:

答案 0 :(得分:1)

到目前为止,我找到的最佳解决方案是使用变压器。 对于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)
}