英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论