Java 个对象列表到 Cucumber DataTable 以执行差异

List of Java Objects to Cucumber DataTable to perform diff

让我们有以下特征文件,

Feature: Search Employees

  Background: 
    Given following employees exists
      | id | name   | department | 
      | 1  | Jack   | HR         | 
      | 2  | Rachel | Finance    | 
      | 3  | Mike   | HR         | 
      | 4  | Emma   | IT         | 

  Scenario: Get Employees By Department

    Given user wants to get list employees in a department

     When searched for department = 'HR'

     Then following list of employees are returned
      | id | name | department | 
      | 1  | Jack | HR         | 
      | 3  | Mike | HR         | 

想象一下,接下来的步骤调用一个 REST 端点,其中 returns 一个 JSON。

When searched for department = 'HR'

这里是休息区JSON,

[
  {
    "id": 1,
    "name": "Jack",
    "department": "HR"
  },
  {
    "id": 3,
    "name": "Mike",
    "department": "HR"
  }
]

对应JavaClass,

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Employee {

  private Integer id;
  private String name;
  private String department;

}

在旧版本的 cucumber(即 1.2.4)中,我们可以如下 DataTable.diff(List actual),

@Then("^following list of employees are returned$")
public void following_list_of_employees_are_returned(DataTable expectedEmployees) throws Throwable {

  List<Map<String, Object>> actualEmployees = new ArrayList<>();
  List<Employee> employees = response.as(Employee[].class);

  employees
      .forEach(e -> {
        Map<String, Object> map = new HashMap<>();
        map.put("id", e.getId());
        map.put("name", e.getName());
        map.put("department", e.getDepartment());

        actualEmployees.add(map);
      });

  expectedEmployees.unorderedDiff(actualEmployees);
}

目前我们升级到以下黄瓜版本,

<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-java8</artifactId>
  <version>4.0.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-spring</artifactId>
  <version>4.0.0</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>io.cucumber</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>4.0.0</version>
  <scope>test</scope>
</dependency>

[INFO] +- io.cucumber:cucumber-java8:jar:4.0.0:test
[INFO] |  +- io.cucumber:cucumber-java:jar:4.0.0:test
[INFO] |  \- net.jodah:typetools:jar:0.5.0:test
[INFO] +- io.cucumber:cucumber-spring:jar:4.0.0:test
[INFO] \- io.cucumber:cucumber-junit:jar:4.0.0:test
[INFO]    \- io.cucumber:cucumber-core:jar:4.0.0:test
[INFO]       +- io.cucumber:cucumber-html:jar:0.2.7:test
[INFO]       +- io.cucumber:gherkin:jar:5.1.0:test
[INFO]       +- io.cucumber:tag-expressions:jar:1.1.1:test
[INFO]       +- io.cucumber:cucumber-expressions:jar:6.1.0:test
[INFO]       \- io.cucumber:datatable:jar:1.1.3:test
[INFO]          \- io.cucumber:datatable-dependencies:jar:1.1.3:test

PROBLEM: In cucumber 1.2.4 versions, DataTable can be diff'ed with a List<Map<String, String>. In the newer version (4.0.0), DataTable.diff expects a DataTable as argument and there is no method to support diff'ing List.

Now, we need to create a datatable object from List<Map<String, String>. so that we can do expectedDataTable.diff(actualDataTable).

QUESTION: Is there a easy way to convert Array of JSON Object or List<JavaObject> to a DataTable so that we can do diff of 2 datatables without creating List<List<String>> from list of objects which requires a lot of code.

完全披露:我为 Cucumber 编写了数据 table 模块。

手动将对象映射到数据 tables 非常耗时、乏味且容易出错。这最好留给对象映射器。此外,用于比较 table 和 List<Map<String, String>> 的实现包含魔法、火焰喷射器和陷阱。所以我觉得最好还是别提了。

解决方案 1

您想做的第一件事是upgrade to v4.2.0

然后将以下配置放在粘合路径的某处。对象映射器来自 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> T transform(Map<String, String> map, Class<T> aClass, TableCellByTypeTransformer tableCellByTypeTransformer) {
            return objectMapper.convertValue(map, aClass);
        }

        @Override
        public <T> T transform(String s, Class<T> aClass) {
            return objectMapper.convertValue(s, aClass);
        }
    }
}

然后将@Getter@Setter替换为@Data,这样hashcodeequalstoString都实现了

@Data
public class Employee {

  private Integer id;
  private String name;
  private String department;

}

然后修改您的步骤以使用员工列表而不是数据 table。上一步中安装的对象映射器将处理从数据 table 到对象的转换。

@Then("^following list of employees are returned$")
public void following_list_of_employees_are_returned(List<Employee> expectedEmployees) throws Throwable {    
  List<Map<String, Object>> actualEmployees = new ArrayList<>();
  List<Employee> employees = response.as(Employee[].class);
  assertEquals(expectedEmployees, actualEmployees);
}

要使比较顺序不敏感,请考虑使用 AssertJs assertThat 而不是 JUnits assertEquals - 它通常带有 Spring

解决方案 2

datatable-matchers 添加到您的依赖项中

<groupId>io.cucumber</groupId>
<artifactId>datatable-matchers</artifactId>

创建您自己的数据 table 并使用 DataTableHasTheSameRowsAs 匹配器进行比较。

@Then("^following list of employees are returned$")
public void following_list_of_employees_are_returned(DataTable expectedEmployees) {
    List<Employee> employees = response.as(Employee[].class);

    DataTable actualEmployees = createTable(
            employees,
            asList("id", "name", "department"),
            Employee::getId, Employee::getName, Employee::getDepartment
    );
    assertThat(actualEmployees, hasTheSameRowsAs(expectedEmployees));
}


static <T> DataTable createTable(List<T> values, List<String> headers, Function<T, Object>... extractors) {
    List<List<String>> 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);
}

很好的回答,我有和你一样的解决方案,但我遇到了一个情况: 假设我们有:

public class Employee {

  private Integer id;
  private String name;
  private List<String> department;

}

有什么方法可以做这样的测试:

     Then following list of employees are returned
      | id | name | department | 
      | 1  | Jack | HR, IT     | 
      | 3  | Mike | HR         | 

意思是如何告诉黄瓜我有部门的名单?

顺便说一句,对于你的具体情况,因为你没有使用 List 或 Hashset,你可以只使用:

    @DefaultParameterTransformer
    @DefaultDataTableEntryTransformer(replaceWithEmptyString = "[blank]")
    @DefaultDataTableCellTransformer
    public Object transformer(Object fromValue, Type toValueType) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(fromValue, objectMapper.constructType(toValueType));
    }

并将其添加到步骤定义中,无需创建 ParameterTypes class