Cucumber 4.7.2 将 table 转换为对象行

Cucumber 4.7.2 transform table to row to object

我正在使用 JAVA Cucumber 4.7.2

我正在将黄瓜 table 中的数据转换为 objects/models。

黄瓜场景:

  Scenario: test
    Given Create company
      | NAME  | ADDRESS        |
      | Apple | Some address 1 |

执行步骤:

And("^Create company$", (DataTable table) ->
{
    List<Company> companies = table.asList(Company.class);
    companies.forEach(c -> c.createModel());
});

公司型号:

public class Company
{
    private String name;
    private String address;
    private Map<String, String> rowData;

    public Company(Map<String, String> rowData)
    {
        this.rowData = rowData;
    }

    public void createModel()
    {
        name = getRowValue("COMPANY");
        address = getRowValue("ADDRESS");
    }

    public String getRowValue(String header)
    {
        String value = rowData.get(header);

        if (Strings.isNullOrEmpty(value))
        {
            throw new NullPointerException("Value for header [" + header + "] is required, but was NULL");
        }
        return value;
    }
}

DataTableConfigurer

public class DataTableConfigurer implements TypeRegistryConfigurer
{
    @Override
    public Locale locale()
    {
        return Locale.ENGLISH;
    }

    @Override
    public void configureTypeRegistry(TypeRegistry registry)
    {
        registry.defineDataTableType(new DataTableType(Company.class, Company::new));
    }
}

现在工作正常,数据从 cucumber table 加载并转换为模型。

我正在尝试修改上面的内容,以允许 Company 模型具有空构造函数,并传递 rowData 方法,例如:

public class Company
{
    private String name;
    private String address;
    private Map<String, String> rowData;

    // no / empty constructor

    /*
     * Passing rowData using method, not by constructor
     */
    public void setRowData(Map<String, String> rowData)
    {
        this.rowData = rowData;
    }

    public String getRowValue(String header)
    {
        String value = rowData.get(header);

        if (Strings.isNullOrEmpty(value))
        {
            throw new NullPointerException("Value for header [" + header + "] is required, but was NULL");
        }
        return value;
    }
}

但我不知道如何编辑 DataTableConfigurer 以允许这样做。

是这样的吗?

typeRegistry.defineDataTableType(new DataTableType(Company.class,  (Map<String, String> entry) -> {
    Company o = new Company();
    o.setRowData(entry);
    return o;
}));