'Converting Datatable to object with null values
Hi Everyone just wondering if I am following best practices here.
I have step defintions like the following
public class StepDefinitions {
@DataTableType
public Author authorEntry(Map<String, String> entry) {
return new Author(
entry.get("firstName"),
entry.get("lastName"),
entry.get("famousBook"));
}
@Given("There are my favorite authors")
public void these_are_my_favourite_authors(List<Author> authors) {
// step implementation
}
}
and my feature file could then be something like
Feature: this is a feature
Scenario: this is a scenario
Given There are my favorite authors
|firstName| lastName |
| first | last |
Scenario: this is another scenario
Given There are my favorite authors
|firstName| lastName | famousBook |
| first | last | book |
So in the first step it will create an Author object but with famousBook == null.
Since I am creating objects used for REST requests and jackson will ignore null values is it okay to create objects like this?
Solution 1:[1]
You are using wrong data structure for your examples. You can refer to this resource giving the examples of different table types.
In your case you have a table
|firstName| lastName |
| first | last |
and try to parse it into Map<String, String> entry
which would result in the following map:
[key1 = firstName, value1 = lastName]
[key2 = first, value2 = last]
If you need to treat it like header in top line then you need to parse it into List<Map<String, String>>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Alexey R. |