Java - Gherkin 和 Cucumber:在垂直方向 table 而不是水平方向传递对象或对象列表
Java - Gherkin & Cucumber: Pass an object or list of objects on a vertical table instead of horizontal
我的功能文件中有以下示例小黄瓜场景:
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
在我的胶水文件中,我可以将数据 table 映射到对象 Trade
作为开箱即用的黄瓜解决方案:
@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
}
我想达到的效果:
通过执行以下操作提高我的小黄瓜场景的可读性:
垂直转置数据 table,如果我的对象有大约 10 个字段,这将提高可读性
用别名替换字段/列名
示例:
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
我希望 table 是动态的,它可以包含多于或少于 2 个数据集/列。实现此目标的最佳方法是什么?
附加信息:
语言:Java 8
黄瓜版本:1.2.5
Trade
POJO 类似于:
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....
*/
}
如果 table 在您的功能文件中指定为
|buyCcy | EUR | GBP |
|sellCcy | USD | EUR |
|amount | 12345.67 | 67890.12 |
|date | 23-11-2017 | 24-11-2017 |
您可以使用以下胶水代码(与您发布的 Trade
class,假设已实施适当的 toString()
方法)
@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);
}
输出
Trade{buyCcy=EUR, sellCcy=USD, amount=12345.67, date=23-11-2017}
Trade{buyCcy=GBP, sellCcy=EUR, amount=67890.12, date=24-11-2017}
我的功能文件中有以下示例小黄瓜场景:
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
在我的胶水文件中,我可以将数据 table 映射到对象 Trade
作为开箱即用的黄瓜解决方案:
@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
}
我想达到的效果:
通过执行以下操作提高我的小黄瓜场景的可读性:
示例:
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
我希望 table 是动态的,它可以包含多于或少于 2 个数据集/列。实现此目标的最佳方法是什么?
附加信息:
语言:Java 8
黄瓜版本:1.2.5
Trade
POJO 类似于:
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....
*/
}
如果 table 在您的功能文件中指定为
|buyCcy | EUR | GBP |
|sellCcy | USD | EUR |
|amount | 12345.67 | 67890.12 |
|date | 23-11-2017 | 24-11-2017 |
您可以使用以下胶水代码(与您发布的 Trade
class,假设已实施适当的 toString()
方法)
@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);
}
输出
Trade{buyCcy=EUR, sellCcy=USD, amount=12345.67, date=23-11-2017}
Trade{buyCcy=GBP, sellCcy=EUR, amount=67890.12, date=24-11-2017}