简体   繁体   中英

Cucumber Java Convert Data Table to Specific Type

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.

There is an old issue regarding this problem in the cucumber-jvm repo on GitHub. It was recently labeled as a bug and is expected to be solved in release 2.1.

https://github.com/cucumber/cucumber-jvm/issues/741

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM