是否可以将Java-Enum作为参数从黄瓜功能文件传递(以更文本友好的方式)? [英] Is it possible to pass Java-Enum as argument from cucumber feature file (in a more text friendly way)?

查看:112
本文介绍了是否可以将Java-Enum作为参数从黄瓜功能文件传递(以更文本友好的方式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,提供的示例似乎将特征文件中的文本锁定为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|

我认为枚举可能是某物ng like:

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

}

如何从其他?

更复杂的示例

在一个更复杂的示例中,我们将将其传递给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)标记枚举不足以使转换器在<$中工作c $ c> 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)
}

这篇关于是否可以将Java-Enum作为参数从黄瓜功能文件传递(以更文本友好的方式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆