javaguy
javaguy

Reputation: 1133

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

I am using cucumber 7 and I have the following Then statement in my step definition file:

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

This gives me the following exception:

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)

It seems only Map<String, String> is allowed.

Any suggestion(s).

Upvotes: 0

Views: 875

Answers (2)

M.P. Korstanje
M.P. Korstanje

Reputation: 12039

Cucumber doesn't know how to turn strings into enums. So as the exception message explains you have to register a data table type:

public class StepDefinitions {

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

Upvotes: 2

javaguy
javaguy

Reputation: 1133

I found a simple solution as below :

@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));
}

Upvotes: 0

Related Questions