Cucumber Java Convert data table to specific type
I want to use a data table in my properties file and convert the content to one specific type in the step definition. For example, I would like to be able to transform the following data table into one instance of the User class.
Given the user is
| firstname | lastname | nationality |
| Roberto | Lo Giacco | Italian |
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 assumes that it is possible to convert a data table into a single object.
However, checking 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 transpose):
Type itemType = listItemType(type);
if (itemType == null) {
throw new CucumberException("Not a Map or List type: " + type);
}
I know this 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 (not in this simplified example) there is only one object that needs to be created and I want to avoid using the list and then take the first item.
source to share