Cucumber 6.9 java8,无法从 DataTable 转换为自定义类型

Cucumber 6.9 java8, cannot convert from DataTable to custom type

我想注册我的自定义类型,以便自动从数据表中获取列表。

尽管有很多来源提到 TypeRegistryConfigurer class,但在最新的 Cucumber 核心版本中似乎已弃用。

我试图查看 this source 所以我这样做了:

public class BurpStepDefs implements En {

    private static ObjectMapper objectMapper = new ObjectMapper();

    private static final DataTableType ENTRY =
            new DataTableType(CartInput.Article.class, (java.util.Map<String, String> entry) ->
                    objectMapper.convertValue(entry, CartInput.Article.class));

    private final DataTableTypeRegistry registry = new DataTableTypeRegistry(Locale.ENGLISH);

        public BurpStepDefs() {

        registry.defineDataTableType(ENTRY);

        Given("burped", () -> {

        });

        When("burping", (DataTable o) -> {
            o.asList(CartInput.Article.class); // goes wrong
        });

        Then("burp", (DataTable o) -> {

        });

    }
}

这是功能文件

Feature: operate with customer cart
  Verify all cart operations

  Scenario: scenario burp
    Given burped
    When burping
      | BIRP        | 1 |
      | BYRP        | 1 |
    Then I burp
      | BIRP        | 1 |
      | BYRP        | 1 |

我收到以下错误:

Can't convert DataTable to List<it.infocert.ecommerce.checkout.model.CartInput$Article>.
Please review these problems:

 - There was no table entry or table row transformer registered for it.infocert.ecommerce.checkout.model.CartInput$Article.
   Please consider registering a table entry or row transformer.

 - There was no default table entry transformer registered to transform it.infocert.ecommerce.checkout.model.CartInput$Article.
   Please consider registering a default table entry transformer.

Note: Usually solving one is enough

有什么提示吗?

编辑

看完@M.P。 Korstanje 和 cucumber specs(起初我认为文档已被弃用)我用这个解决了:

DataTableType((java.util.List<String> entry) -> {

    final var output = new CartInput.Article();
    output.setName(entry.get(0));
    output.setQuantity(Integer.parseInt(entry.get(1)));
    output.setOperationId("A");
    return output;
});

您正在使用来自 Cucumbers 内部 API 的点点滴滴。然而,整个 Cucumbers lambda DSL 都包含在 En 接口(及其父接口 LambdaGlue)中。因此,要定义单个数据 table 入口转换器,您可以这样做:

public class BurpStepDefs implements En {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public BurpStepDefs()
        DataTableType(
                (Map<String, String> entry) -> objectMapper.convertValue(entry, CartInput.Article.class));
    }
}

如果您使用的是现代 IDE,您还可以在构造函数中键入 this. 并触发自动完成 (ctrl+space) 查看可用的建议。