代码之家  ›  专栏  ›  技术社区  ›  Stéphane GRILLON

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

  •  1
  • Stéphane GRILLON  · 技术社区  · 5 年前

    我的小黄瓜剧本:

    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$3.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;
                }
            }));
        }
    
    }
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   M.P. Korstanje    5 年前

    您已经注册了 TableEntryTransformer 通用的 Map 输入而不是 Map<String, Boolean> 我怀疑你打算这么做。

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

    请使用TableEntryTransformer或TableCellTransformer for java.lang.Boolean注册DataTableType。

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

    此外,因为您的表顶部没有标题,但您必须将表转置到侧面。这将使您的桌子变成:

    |yes|no|
    |true|false|
    

    可以在要素文件中重新定位表格,也可以使用 @cucumber.api.Transpose values 参数:

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