我想使用Java(而不是Gherkin)手动设置Cucumber DataTable。
在小黄瓜,我的桌子看起来像这样:
| h1 | h2 |
| v1 | v2 |
到目前为止,我的Java看起来像这样:
List<String> raw = Arrays.asList( "v1", "v2");
DataTable dataTable = DataTable.create(raw, Locale.getDefault(), "h1", "h2");
我得到的是带有标题但没有内容的DataTable。它也比预期的要长:
| h1| h2 |
| | |
| | |
我确信解决方案必须相当简单,但我现在有点不知所措。我需要做什么才能拿到我的桌子?
答案 0 :(得分:11)
希望这会有所帮助。如果你已经满了Gherkins步骤就像这样......
When I see the following cooked I should say:
| food | say |
| Bacon | Yum! |
| Peas | Really? |
你想在Java中使用它。 (请注意,传入的cucumber.api.DataTable在测试前设置了您的预期值。
@When("^I see the following cooked I should say:$")
public void theFoodResponse(DataTable expectedCucumberTable) {
// Normally you'd put this in a database or JSON
List<Cuke> actualCukes = new ArrayList();
actualCukes.add(new Cuke("Bacon", "Yum!"));
actualCukes.add(new Cuke("Peas", "Really?"));
Another link to a Full Example.diff(actualCukes)
}
我会说,在AslakHellesøy的例子中,他实际上并没有使用DataTable。
他会做这样的例子:
@When("^I see the following cooked I should say:$")
public void theFoodResponse(List<Entry> entries) {
for (Entry entry : entries) {
// Test actual app you've written
hungryHuman.setInputValue(entry.food);
hungryHuman.setOutputValue(entry.say);
}
}
public class Entry {
String food;
String say;
}
有关更多阅读的完整示例,请查看:
修改强>
对于矫枉过正的@Christian,您可能不需要在应用程序中使用它的整个上下文,只是一种干净的方式来使用 DataTable.create 以及我发布的大部分内容是另一种使用Entry类对那只猫进行皮肤修饰的方法(对于那些稍后阅读它的人来说可能会有所帮助。)
所以你在评论中这样做的方式并不遥远。我不是专业人士,所以我不能给你任何关于制作你的2D字符串列表的提示,但我可以澄清最后两个参数(如果你使用全部4个)。
List<List<String>> infoInTheRaw = Arrays.asList( Arrays.asList("h1", "h2"),
Arrays.asList("v1", "v2") );
DataTable dataTable = DataTable.create(infoInTheRaw);
你也可以使用同样混乱的构造函数。 :)
答案 1 :(得分:3)
我们实际上可以使用与数据表中相同的字段创建自定义对象列表而不是DataTable。
例如。
delegateEvents
现在,您的Step类应该将步骤定义为
Then the book list response should contain records as
| book_id | book_title | book_cost |
| B0001 | Head First Java | 350|
| B002 | Head First Spring| 550|
您的Book类应该看起来像
@Then("^the book list response should contain records as$")
public void validateBookRecords(List<Book> booksList) throws Throwable { //assertions will go here}
由于这些字段都是公开的,因此不需要getter / setter。如果您决定将它们声明为私有(不明白原因),那么请确保添加getter / setter。
希望这有帮助