黄瓜在从info.cukes更新到io.cucumber后不允许无效的枚举。

huangapple go评论52阅读模式
英文:

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

问题

# 测试定义的功能文件
功能:解析枚举

  场景:使用空字符串解析枚举
    假设 我正在解析枚举“”
//步骤定义代码
    @假设("^我正在解析枚举\"(.*?)\"$")
    public void i_m_parsing_enum(MyEnum arg1) throws Throwable {
        System.out.println(arg1); // 应该打印null,但在库升级后会抛出异常
    }
//简单的枚举
public enum MyEnum {
    abc, def
}
英文:

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
}

答案1

得分: 3

以下是您要的翻译内容:

根据 @luis-iñesta 的评论,我使用 @ParameterType 注解编写了简单的 try-catch:

    @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(MyEnum arg1) throws Throwable {
        System.out.println(arg1);
    }

在这种情况下可以工作,但我仍然想知道是否有某个开关或参数可以保留旧的 Cucumber 行为。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月14日 18:49:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64351671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定