Cucumber Java Convert Data Table to Specific Type

Viewed 5315

I want to use a data table in my feature file and have the contents converted to a single specific type in the step definition. For Example I would like to be able to convert the following data table into a single instance of the User class.

 Given the user is
    | firstname  | lastname  | nationality |
    | Roberto    | Lo Giacco | Italian     |

The User class:

class User {
    String firstName;
    String lastName;
    Nationality nationality;
//getters / setters / etc
}

Step Definition:

 @Given("^the user is$")
 public void the_user_is(User user) {
     this.user = user;
 }

However when I run this I get the following error:

cucumber.runtime.CucumberException: cucumber.runtime.CucumberException: Not a Map or List type: class example.User

The documentation suggests that it is possible to convert a data table to a single object.

Cucumber Transpose API Docs

However from inspecting the code it looks like it will always return a list. This code snippet is from cucumber/runtime/table/TableConverter.convert(DataTable dataTable, Type type, boolean transposed) :

Type itemType = listItemType(type);
if (itemType == null) {
    throw new CucumberException("Not a Map or List type: " + type);
}

I know it works for a list:

 @Given("^the user is$")
 public void the_user_is(List<User> user) {
     this.user = user.get(0);
 }

but in my real world step (rather than this simplified example) there is only ever one object that needs created and I want to avoid using a list and then taking the first item.

1 Answers
Related