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

2022-01-22 00:00:00 enums cucumber java cucumber-jvm

建立在 this question,提供的示例似乎将特征文件中的文本锁定为Java编程风格(注意文本全部大写,并且只有一个单词.

Building up on this question, the example provided seems to lock the text in the feature file too much to Java programming style (notice that the text is written in all uppercase, and is only one word.

当功能文件有更多人类可读"的文本时,是否可以传递枚举?例如:

Is it possible to pass enums when the feature file has more "human readable" text? 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|

我认为枚举可能是这样的:

I reckon the enum could be something like:

public enum Holiday {

CHRISTMAS("Christmas"),NEW_YEAR("New Year"),INDEPENDENCE_DAY("independence-day");

private String extendedName;

private Holidays(String extendedName) {
    this.extendedName = extendedName;
}

}

我们怎样才能从另一个转换?

How could we convert one from the other?

更复杂的例子

在更复杂的示例中,我们会将其传递给 ScenarioObject

In a more complex example, we would pass this onto a 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,如此处所述,将不胜感激如何将其应用于 ScenarioObject 的示例,因为只需使用 标记枚举@XStreamConverter(HolidayTransformer.class) 不足以让转换器在 ScenarioObject 中工作.

Update: If the only solution is to apply a Transformer, as described here, an example of how this would be a applied to the ScenarioObject would be appreciated, since simply tagging the enum with a @XStreamConverter(HolidayTransformer.class) is not sufficient for the transformer to work within the ScenarioObject.

推荐答案

到目前为止,我找到的最佳解决方案是使用变压器.对于带有ScenarioObject的复杂示例,这涉及到:

The best solution I found for this so far was with a transformer. In the case of the complex example with ScenarioObject,this involves:

用转换器标记枚举

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

相关文章