Cucumber 3+ - UndefinedDataTableTypeException:无法将 DataTable 转换为 Map<String, Boolean>

Cucumber 3+ - UndefinedDataTableTypeException: Can't convert DataTable to Map<String, Boolean>

我的小黄瓜脚本:

Then I update checkboxes and check radio list 'DEMO_HOME-agree' with 'yes':
    |yes|true|
    |no|false|

我的黄瓜代码:

@Then("I update checkboxes and check radio list {string} with {string}:")
public void selectCheckbox(String elementKey, String valueKey, Map<String, Boolean> values) throws TechnicalException, FailureException {
    selectCheckbox(this.demoPage.getPageElementByKey(elementKey), true);
    selectCheckbox(this.demoPage.getPageElementByKey(elementKey), valueKey, values);
    checkRadioList(this.demoPage.getPageElementByKey(elementKey), valueKey);
}

我有这个错误:

io.cucumber.datatable.UndefinedDataTableTypeException: Can't convert DataTable to Map<java.lang.String, java.lang.Boolean>.
Please register a DataTableType with a TableEntryTransformer or TableCellTransformer for java.lang.Boolean.
    at io.cucumber.datatable.UndefinedDataTableTypeException.mapNoConverterDefined(UndefinedDataTableTypeException.java:24)
    at io.cucumber.datatable.DataTableTypeRegistryTableConverter.convertEntryValues(DataTableTypeRegistryTableConverter.java:333)
    at io.cucumber.datatable.DataTableTypeRegistryTableConverter.toMap(DataTableTypeRegistryTableConverter.java:209)
    at io.cucumber.datatable.DataTableTypeRegistryTableConverter.convert(DataTableTypeRegistryTableConverter.java:63)
    at io.cucumber.datatable.DataTable.convert(DataTable.java:362)
    at io.cucumber.stepexpression.StepExpressionFactory.transform(StepExpressionFactory.java:73)
    at io.cucumber.stepexpression.DataTableArgument.getValue(DataTableArgument.java:19)
    at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:68)
    at cucumber.runner.TestStep.executeStep(TestStep.java:63)
    at cucumber.runner.TestStep.run(TestStep.java:49)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
    at cucumber.runner.TestCase.run(TestCase.java:44)
    at cucumber.runner.Runner.runPickle(Runner.java:40)
    at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:146)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)

在我的 DataTableConfigurer 中:

public class DataTableConfigurer implements TypeRegistryConfigurer {

    @Override
    public Locale locale() {
        return Locale.ENGLISH;
    }

    @Override
    public void configureTypeRegistry(TypeRegistry registry) {
        registry.defineDataTableType(new DataTableType(Map.class, new TableEntryTransformer<Map>() {
            @Override
            public Map<String, Boolean> transform(Map<String, String> entries) {
                Map<String, Boolean> newMap = new HashMap<>();
                entries.forEach((k, v) -> newMap.put(k, Boolean.parseBoolean(v)));
                return newMap;
            }
        }));
    }

}

您已经为通用 Map 类型注册了 TableEntryTransformer 而不是 Map<String, Boolean> 我怀疑您打算这样做。

但是错误消息询问您是否愿意

Please register a DataTableType with a TableEntryTransformer or TableCellTransformer for java.lang.Boolean.

您可以通过以下方式完成:

@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
    typeRegistry.defineDataTableType(
            new DataTableType(
                    Boolean.class,
                    Boolean::parseBoolean
            ));
}

此外,因为您的 table 没有顶部的 headers,但您必须将 table 移到侧面。这会将您的 table 变成:

|yes|no|
|true|false|

您可以在特征文件中重新定位 table 或对 values 参数使用 @cucumber.api.Transpose 注释:

public void selectCheckbox(String elementKey, String valueKey, @Transpose Map<String, Boolean> values) throws TechnicalException, FailureException {