gpt4 book ai didi

java - Cucumber 4.7.2 将表转换为行到对象

转载 作者:行者123 更新时间:2023-12-02 09:04:14 24 4
gpt4 key购买 nike

我使用的是JAVA Cucumber 4.7.2

我正在将数据从 Cucumber 表转换为对象/模型。

cucumber 场景:

  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 表加载,并转换为模型。

我正在尝试修改上面的内容,以允许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;
}));

关于java - Cucumber 4.7.2 将表转换为行到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com