黄瓜解析表到对象列表

时间:2017-03-01 11:44:59

标签: cucumber bdd serenity-bdd cucumber-serenity

在黄瓜中,我可以将表解析为对象列表,例如:

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

我的专题文件:

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

我的继续课程:

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

当我的模型对象引用其他对象时,我该怎么做呢,例如:

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;
}

在这种情况下,如何编写表格以便解析模型列表?

1 个答案:

答案 0 :(得分:0)

仅按以下方式使用。会起作用

@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);
}