Is it possible to pass Java-Enum as argument from

2019-08-02 17:37发布

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.:

Simple Example

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?

More complex example

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

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.

1条回答
Deceive 欺骗
2楼-- · 2019-08-02 18:09

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

Mark enum with converter

@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 {...}
}

Create the transformer

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

}

Mark the ScenarioObject with the transformer as well

public class ScenarioObject{
private String character;
@XStreamConverter(HolidayTransformer.class)
private Holiday holiday;
(...getters and setters)
}
查看更多
登录 后发表回答