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

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

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

问题

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

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.

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

答案1

得分: 3

以下是您要的翻译内容:

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

  1. @ParameterType("[a-z]*")
  2. public MyEnum myEnum(String name) {
  3. try {
  4. return MyEnum.valueOf(name);
  5. } catch (IllegalArgumentException e){
  6. return null;
  7. }
  8. }
  9. @Given("I'm parsing enum \"{myEnum}\"")
  10. public void i_m_parsing_enum(MyEnum arg1) throws Throwable {
  11. System.out.println(arg1);
  12. }

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

英文:

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

  1. @ParameterType("[a-z]*")
  2. public MyEnum myEnum(String name) {
  3. try {
  4. return MyEnum.valueOf(name);
  5. } catch (IllegalArgumentException e){
  6. return null;
  7. }
  8. }
  9. @Given("I'm parsing enum \"{myEnum}\"")
  10. public void i_m_parsing_enum(MyEnumo arg1) throws Throwable {
  11. System.out.println(arg1);
  12. }

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:

确定