I have the following sample gherkin scenario on my feature file:
Scenario: Book an FX Trade
Given trades with the following details are created:
|buyCcy |sellCcy |amount |date |
|EUR |USD |12345.67 |23-11-2017 |
|GBP |EUR |67890.12 |24-11-2017 |
When the trades are executed
Then the trades are confirmed
In my glue file, I can map the data table to an object Trade
as an out of the box cucumber solution:
@When("^trades with the following details are created:$")
public void trades_with_the_following_details_are_created(List<Trade> arg1) throws Throwable {
//do something with arg1
}
What I want to achieve:
Improve the readability of my gherkin scenario by doing the following:
Sample:
Scenario: Book an FX Trade
Given trades with the following details are created:
|Buy Currency | EUR | GBP |
|Sell Currency | USD | EUR |
|Amount | 12345.67 | 67890.12 |
|Date | 23-11-2017 | 24-11-2017 |
When the trades are executed
Then the trades are confirmed
I want the table to be dynamic in a way that it can have more or less than 2 data sets / columns. What would be the best way to achieve this?
Additional info:
Language: Java 8
Cucumber version: 1.2.5
Trade
POJO being something like:
public class Trade {
private String buyCcy;
private String sellCcy;
private String amount;
private String date;
/**
* These fields are growing and may have around 10 or more....
* private String tradeType;
* private String company;
*/
public Trade() {
}
/**
* accessors here....
*/
}
If the table is specified in your feature file as
|buyCcy | EUR | GBP |
|sellCcy | USD | EUR |
|amount | 12345.67 | 67890.12 |
|date | 23-11-2017 | 24-11-2017 |
you can use the following glue code (with your posted Trade
class, assuming that there is a proper toString()
method implemented)
@Given("^trades with the following details are created:$")
public void tradeWithTheFollowingDetailsAreCreated(DataTable dataTable) throws Exception {
// transpose - transposes the table from the feature file
// asList - creates a `List<Trade>`
List<Trade> list = dataTable.transpose().asList(Trade.class);
list.stream().forEach(System.out::println);
}
output
Trade{buyCcy=EUR, sellCcy=USD, amount=12345.67, date=23-11-2017}
Trade{buyCcy=GBP, sellCcy=EUR, amount=67890.12, date=24-11-2017}