Is it possible to pass Java-Enum as argument from cucumber feature file (in a more text friendly way)?

邮差的信 提交于 2020-01-15 15:29:26

问题


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:


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


来源:https://stackoverflow.com/questions/49898427/is-it-possible-to-pass-java-enum-as-argument-from-cucumber-feature-file-in-a-mo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!