Search code examples
javaenumscucumber

Cucumber doesn't allow invalid enums after update from info.cukes to io.cucumber


I'm migrating cucumber from old version from package info.cukes to latest io.cucumber. I noticed that old version allows invalid enum values as test arguments and returns null but latest version throws exception io.cucumber.core.exception.CucumberException: Could not convert arguments for step...

How can I keep old behaviour in my tests after migration? Below example code to reproduce error.

#  feature file with test definition
Feature: Parsing enums

  Scenario: Parse enum using empty string
    Given I'm parsing enum ""
//Step definition code
    @Given("^I'm parsing enum \"(.*?)\"$")
    public void i_m_parsing_enum(MyEnum arg1) throws Throwable {
        System.out.println(arg1); // should print null but throws exception after library upgrade
    }
//Simple enum
public enum MyEnumo {
    abc, def
}

Solution

  • Following @luis-iñesta comment I wrote simple try-catch using @ParameterType annotation:

        @ParameterType("[a-z]*")
        public MyEnum myEnum(String name) {
            try {
                return MyEnum.valueOf(name);
            } catch (IllegalArgumentException e){
                return null;
            }
        }
    
        @Given("I'm parsing enum \"{myEnum}\"")
        public void i_m_parsing_enum(MyEnumo arg1) throws Throwable {
            System.out.println(arg1);
        }
    

    Works in this one case but I still wonder if there is some switch or parameter to keep old cucumber behaviour.