全面披露:我为Cucumber编写了数据表模块.
手动将对象映射到数据表以及将对象映射到数据表非常耗时,无聊且容易出错.这最好留给对象映射器.另外,用于比较表与List< Map< String,String>>的实现.包含魔法,喷火器和陷阱.因此,我认为最好将其省略.
解决方案1
然后将以下配置放在粘合路径上的某个位置.对象映射器来自Jackson.它通常随Spring一起提供.
public class ParameterTypes implements TypeRegistryConfigurer {
@Override
public Locale locale() {
return ENGLISH;
}
@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
Transformer transformer = new Transformer();
typeRegistry.setDefaultDataTableCellTransformer(transformer);
typeRegistry.setDefaultDataTableEntryTransformer(transformer);
typeRegistry.setDefaultParameterTransformer(transformer);
}
private class Transformer implements ParameterByTypeTransformer, TableEntryByTypeTransformer, TableCellByTypeTransformer {
ObjectMapper objectMapper = new ObjectMapper();
@Override
public Object transform(String s, Type type) {
return objectMapper.convertValue(s, objectMapper.constructType(type));
}
@Override
public T transform(Map map, Class aClass, TableCellByTypeTransformer tableCellByTypeTransformer) {
return objectMapper.convertValue(map, aClass);
}
@Override
public T transform(String s, Class aClass) {
return objectMapper.convertValue(s, aClass);
}
}
}
然后将@Getter和@Setter替换为@Data,以便实现哈希码,equals和toString.
@Data
public class Employee {
private Integer id;
private String name;
private String department;
}
然后修改您的步骤以使用雇员列表而不是数据表.上一步中安装的对象映射器将处理从数据表到对象的转换.
@Then(“^following list of employees are returned$”)
public void following_list_of_employees_are_returned(List expectedEmployees) throws Throwable {
List> actualEmployees = new ArrayList<>();
List employees = response.as(Employee[].class);
assertEquals(expectedEmployees, actualEmployees);
}
为了使比较顺序不敏感,请考虑使用AssertJs assertThat而不是JUnits assertEquals-它通常随Spring一起提供
解决方案2
将datatable-matchers添加到您的依赖项
io.cucumber
datatable-matchers
创建您自己的数据表并使用DataTableHasTheSameRowsAs匹配器对其进行比较.
@Then(“^following list of employees are returned$”)
public void following_list_of_employees_are_returned(DataTable expectedEmployees) {
List employees = response.as(Employee[].class);
DataTable actualEmployees = createTable(
employees,
asList(“id”, “name”, “department”),
Employee::getId, Employee::getName, Employee::getDepartment
);
assertThat(actualEmployees, hasTheSameRowsAs(expectedEmployees));
}
static DataTable createTable(List values, List headers, Function… extractors) {
List> rawTable = new ArrayList<>();
rawTable.add(headers);
values.stream()
.map(employee -> Stream.of(extractors)
.map(f -> f.apply(employee))
.map(String::valueOf)
.collect(Collectors.toList()))
.forEach(rawTable::add);
return create(rawTable);
}
今天的文章date比较大小 java_java比较两个list中对象的值分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/86483.html