How to create a cucumber data table?

I want to manually configure Cucumber DataTable using Java (instead of Gherkin).

In Gherkin, my table will look like this:

| h1 | h2 | | v1 | v2 | 

My Java looks like this:

 List<String> raw = Arrays.asList( "v1", "v2"); DataTable dataTable = DataTable.create(raw, Locale.getDefault(), "h1", "h2"); 

What I get is a DataTable with headers but no content. This is also longer than expected:

  | h1| h2 | | | | | | | 

I am sure that the solution should be quite simple, but now I'm a little confused. What do I need to do to get the table?

+8
cucumber cucumber-jvm gherkin
source share
2 answers

Hope this helps. If you are a full step of worming it looks like this ...

 When I see the following cooked I should say: | food | say | | Bacon | Yum! | | Peas | Really? | 

You want it in Java. (Note that cucumber.api.DataTable went through the 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 of Aslak Hellesoy he actually does not use DataTable.

He would make 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 complete example for additional validation:

  • Millstone calculator: link
  • Java Calculator Step Definition: Link

EDIT:

Sorry for the overkill @Christian, you may not need the whole context of how to use it in the application, just a clean way to use DataTable.create, and most of what I wrote is another way to infect this cat with Entry class (which may be useful for those reading this later.)

So you did it in your comment just around the corner. I am not a pro in collections, so I cannot give you any advice on creating your 2D list of strings, but I can clarify the last two parameters (if you use all 4).

  • If your columns use the Date or Calendar fields, you can specify Format in the parameter from the second to the last .
  • If you do not use the latter for column names, then it will automatically read your top row for column names.
  • You can also remove Locale.getDefault () and it will do it for you; so you look at:

.

 List<List<String>> infoInTheRaw = Arrays.asList( Arrays.asList("h1", "h2"), Arrays.asList("v1", "v2") ); DataTable dataTable = DataTable.create(infoInTheRaw); 

You can also use a constructor that would be just as dirty. :)

+7
source share

Instead of a DataTable, we can actually create a Custom Object List using the same fields as in the data table.

eg,

 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| 

Your Step class should now define 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 all fields are public, a getter / setter is not required for this. If you decide to declare them private (you don’t see the reason why), then make sure that you also add getters / seters.

Hope this helps

+3
source share

All Articles