Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber Java Convert Data Table to Specific Type

Tags:

java

cucumber

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.

like image 636
kceeeeee Avatar asked Jul 21 '17 10:07

kceeeeee


People also ask

What is a DataTable in cucumber?

The DataTable object contains the tabular data from the data table we defined in our scenario as well as methods for transforming this data into usable information. Generally, there are three ways to transform a data table in Cucumber: (1) a list of lists, (2) a list of maps and (3) a table transformer.

How do I convert a table to a list in cucumber?

Cucumber converts the above table into a list of lists by treating each row as a list of the column values. So, Cucumber parses each row into a list containing the book title as the first element and the author as the second: We use the asLists method — supplying a String.class argument — to convert the DataTable argument to a List<List<String>>.

What is cucumber in Java 1?

1. Overview Cucumber is a Behavioral Driven Development (BDD) framework that allows developers to create text-based test scenarios using the Gherkin language. In many cases, these scenarios require mock data to exercise a feature, which can be cumbersome to inject — especially with complex or multiple entries.

Does the first row of a cucumber map contain data?

Please note that the first row does contain data, not table headers, even though the syntax highlighting suggests otherwise! As you can see, Cucumber is able to automatically convert the data table that is passed as an argument to the @Given step to a Map, which is essentially a collection of key-value pairs.


Video Answer


1 Answers

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

like image 104
Luciano van der Veekens Avatar answered Oct 12 '22 16:10

Luciano van der Veekens