What I want
I am working with DataTables in Cucumber.
I have the following situation:
Step in the feature file:
I set the ingredients necessary for a meal
| name | ingredients |
| mac and cheese | pasta,cheese |
| hamburger | bread,meat,lettuce |
In the StepDefinition file I have
@When("I set the ingredients necessary for a meal")
public void setIngredients(List<Meal> meals){
//do things with it
}
And I have a class Meal
public class Meal {
String name;
List<String> ingredients;
}
This doesn't work.
what I know
If I set my ingredients field as a simple String Cucumber "magically" matches both name and ingredients to the fields of the class and, in the step definition I will get a List of Meals correctly filled. But as it currently is it doesn't automatically match.
What I tried
I tried defining the class as:
public class Meal {
String name;
String ingredients;
List<String> ingredientsList;
}
And having a constructor Meal(String, String) that would parse the ingredients into the ingredients list, but it doesn't work.
I tried defining a setter for ingredients that would parse it and also define the ingredientsList but it also doesn't work.
I tried using a DataTable in the step definition but still I can't find a way to transform it into my list of ingredients.
I tried using Transformer but, AFAIK, I would have to define a step for each meal I want to serve and I would have to sent the values within the step itself.
What I don't want
I don't want to be forced to parse the information anywhere but the Meal class.
How I temporarily solved it
Within the more complete Meal definition, a defined a setIngredientsList() that parses the ingredients into a list.
On the step definition, I iterate through the list of meals and call setIngredientsList for each of them. As I said, I don't want any of this processing done outside of the Meal class.
Question
Does anyone know how I can do this please?