如何在 Cucumber 7 中映射枚举和字符串的映射

How to map a Map of enum and String in Cucumber 7

我正在使用 Cucumber 7,我的步骤定义文件中有以下 Then 语句:

    @Then("^with the following Properties:$")
    public void with_the_following_Properties(Map<Gender, String> properties) {
       
    }

这给了我以下异常:

io.cucumber.core.exception.CucumberException: Could not convert arguments for step [^with the following properties:$] defined at 'com.test.glue.TestStepDefs.with_the_following_Properties(java.util.Map<com.test.Gender, java.lang.String>)'.
It appears you did not register a data table type.

    at io.cucumber.core.runner.PickleStepDefinitionMatch.registerDataTableTypeInConfiguration(PickleStepDefinitionMatch.java:96)

似乎只允许使用 Map

任何建议。

我找到了一个简单的解决方案,如下所示:

@Then("^with the following Properties:$")
public void with_the_following_Properties(Map<String, String> properties) {
   
  Map<Gender, String> map = new HashMap<>();
  properties.forEach((k, v) -> map.put(Gender.valueOf(k), v));
}

Cucumber 不知道如何将字符串转换为枚举。因此,正如异常消息所解释的那样,您有 to register a data table type:

public class StepDefinitions {

    @DataTableType
    public Gender authorEntryTransformer(String entry) {
      return Gender.valueOf(entry);
    }
}