I want to manually set up a Cucumber DataTable using Java (instead of Gherkin).
In Gherkin, my table would look like this:
| h1 | h2 |
| v1 | v2 |
My Java so far looks like this:
List<String> raw = Arrays.asList( "v1", "v2");
DataTable dataTable = DataTable.create(raw, Locale.getDefault(), "h1", "h2");
What I get back is a DataTable with headers but no contents. It's also longer than expected:
| h1| h2 |
| | |
| | |
I'm sure the solution must be fairly simple, but I'm a bit at a loss right now. What do I need to do to get my table?
Instead of DataTable we can actually create List of custom object using the same fields as in data table.
for e.g.
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|
Now your Step class should define a Step as
@Then("^the book list response should contain records as$")
public void validateBookRecords(List<Book> booksList) throws Throwable { //assertions will go here}
And your Book class should look like
public class Book {
public String book_id;
public String book_title;
public String book_cost;
}
Since the fields are all public so no getter/setter are needed. If you decide to declare them private (dont see a reason why) then ensure to add getters/setters as well.
Hope this helps
Hope this helps. If you're full Gherkins step looks like this...
When I see the following cooked I should say:
| food | say |
| Bacon | Yum! |
| Peas | Really? |
You want this in Java. (Note that the cucumber.api.DataTable passed in is setup with your expected values before the test).
@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)
}
I will say that in the examples by Aslak Hellesøy he doesn't actually use the DataTable.
He would do your example something like this:
@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;
}
For a Full Example for more reading check out:
EDIT:
Sorry for the overkill @Christian, you perhaps didn't need the whole context of how to use it in an app, just a clean way to use DataTable.create and most of what I posted was another way to skin that cat with the Entry class (Which might be helpful for those reading this later.)
So the way you did it in your comment wasn't far off. I'm no pro at collections, so I can't give you any tips on making your 2D List of Strings, but I can clarify on the last two parameters (if you're using all 4).
.
List<List<String>> infoInTheRaw = Arrays.asList( Arrays.asList("h1", "h2"),
Arrays.asList("v1", "v2") );
DataTable dataTable = DataTable.create(infoInTheRaw);
You could also use the constructor which would be equally messy. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With