How to convert a HashMap to cucumber dataTable?

I want to know if there is any way to convert a HashMap to a DataTable in java Cucumber. I tried doing some googling and saw that the reverse is possible. Any idea on how to implement this?

Thanks in advance.

Upvotes: 5

Views: 3510

Answers (1)

gargkshitiz
gargkshitiz

Reputation: 2168

Considering that

  1. All the data is kept in the map as Strings and
  2. You want a single row in your data table, wherein headers would be keys and row items would be values from the input hashmap. If the conversion would have been other way around, then you would have a list of Maps as Data table would have multiple rows (values for different maps) against the columns (keys).

You could try:

List<List<String>> data = Arrays.asList(new ArrayList<String>(map.keySet()), new ArrayList<String>(map.values())); 
DataTable dataTable = DataTable.create(data);

P.S. I have not tested the syntax.

Upvotes: 3

Related Questions