1

In cucumber I can parse a table to a list of a objects, for example:

public class Model {
    public String a;
    public String b;
    public String c;
}

My feature file:

Scenario: Example
Given examples
|a     |b      |c    |
|lsj   |fjsldkf|fljs |
|fjsdfj|jfkskjl|fsjls|

My step class:

@Given("$examples$")
public void examples(List<Model> list){}

How can I do same when my model object has reference for other objects, for instance:

public class SubModel{
    public String subA;
    public String subB;
}

public class Model {
    public String a;
    public String b;
    public String c;
    public SubModel subModel1;
    public SubModel subModel2;
}

In this case how can I write a table so I can parse to a list of models?

Rogger Fernandes
  • 805
  • 4
  • 14
  • 28
  • 1
    You will need to manually create the Model objects. Cucumber is not able to handle creation of classes containing other classes. As cucumber uses reflection to populate the fields, instead of calling a constructor you will not be able to create your submodel objects. I would create a class which takes in all the required data into string fields and has a method which returns the properly created Model object. – Grasshopper Mar 01 '17 at 16:49

1 Answers1

0

just use as below. It will work

@Given("^examples$")
public void examples(DataTable sampleTable) {
    Map<String, String> sampleMap = new HashMap<>();
    DataTableRow header = sampleTable.getGherkinRows().get(0);
    DataTableRow value = sampleTable.getGherkinRows().get(1);
    for (String key : header.getCells()) {
        sampleMap.put(key, value.getCells().get(header.getCells().indexOf(key)));
    }

    client.setSampleDetails(sampleMap);
}
Siva
  • 113
  • 4
  • 13