Cucumber-JVM 3 - io.cucumber.datatable.UndefinedDataTableTypeException

后端 未结 4 1966
无人及你
无人及你 2020-12-18 05:03

I updated from Cucumber-JVM 2.4.0 to 3.0.2 in my pom.xml and DataTables started throwing this exception:

io.cucumber.datatable.UndefinedD

相关标签:
4条回答
  • 2020-12-18 05:23
        I have created a code which won't use **DataTable** concept. You can update this below implementation so that you won't get any failures in your scripts in future.
    
        CucumberUtil.java:
        -----------------
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;
    
        public class CucumberUtil {
    
    // public static synchronized Map<String, String> TableDictionaryConverter(DataTable table) {                    -- removed this concept because of version issues in DataTable
       public static synchronized Map<String, String> TableDictionaryConverter(List<List<String>> data) {
              Map<String, String> mapTable = new HashMap<String, String>();
              for(List<String> rows: data) {
                     mapTable.put(rows.get(0), rows.get(1)); 
              }
              return mapTable;
       }
    
    // Feature
    When I Enter My Regular Income Sources
      | name     | Salary        | 
      | amount   | 25000         | 
      | frequency| every 2 weeks |
    
    // Stepdef
    @When("^I Enter My Regular Income Sources$")
    public void I_Enter_My_Regular_Income_Sources(List<List<String>> table) throws Throwable {
    
        Map<String, String> mapTable = CucumberUtil.TableDictionaryConverter(table);
    
        String nameValue = mapTable.get("name");         // Salary
        String amountValue = mapTable.get("name");       // 25000
        String frequencyValue = mapTable.get("name");    // every 2 weeks
    
        // More code    
    }
    
    0 讨论(0)
  • 2020-12-18 05:24

    Migrating from v2.x.x to v3.x.x for DataTable

    Posting my answer to serve as reference for those who may encounter the same. For their release announcement, click here.

    I decided to put DataTableConfigurer.java in its own package so it does not mix with my stepdefs:

    Runner:

    @CucumberOptions(features = { "src/test/resources/features" }, tags = { "not @ignore" }, glue = {
            "jcucumberng/steps/defs", "jcucumberng/steps/config", "jcucumberng/steps/hooks" }, ...
    

    DataTableConfigurer:

    import java.util.Locale;
    import java.util.Map;
    
    import cucumber.api.TypeRegistry;
    import cucumber.api.TypeRegistryConfigurer;
    import io.cucumber.datatable.DataTableType;
    import io.cucumber.datatable.TableEntryTransformer;
    import jcucumberng.steps.domain.Expense;
    import jcucumberng.steps.domain.Income;
    
    /*
     * Maps datatables in feature files to custom domain objects.
     */
    public class DataTableConfigurer implements TypeRegistryConfigurer {
    
        @Override
        public Locale locale() {
            return Locale.ENGLISH;
        }
    
        @Override
        public void configureTypeRegistry(TypeRegistry registry) {
            registry.defineDataTableType(new DataTableType(Income.class, new TableEntryTransformer<Income>() {
                @Override
                public Income transform(Map<String, String> entry) {
                    return new Income(entry.get("name"), entry.get("amount"), entry.get("frequency"));
                }
            }));
    
            registry.defineDataTableType(new DataTableType(Expense.class, new TableEntryTransformer<Expense>() {
                @Override
                public Expense transform(Map<String, String> entry) {
                    return new Expense(entry.get("name"), entry.get("amount"), entry.get("frequency"));
                }
            }));
        }
    
    }
    

    I had another custom domain type Expense (which happened to have the same fields), so I just registered it again based on the example.

    0 讨论(0)
  • 2020-12-18 05:26

    It has been totally revamped. XStream has been removed , so earlier code will not work.

    You will need to add logic for datatable and parameter conversion. Refer to these - https://github.com/cucumber/cucumber/tree/master/datatable and https://github.com/cucumber/cucumber/tree/master/cucumber-expressions . Place below class code inside a package defined in the glue option.

    public class Configurer implements TypeRegistryConfigurer {
    
        @Override
                public void configureTypeRegistry(TypeRegistry registry) {
    
        registry.defineDataTableType(new DataTableType(Income.class, new TableEntryTransformer<Income>() {
                        @Override
                        public Income transform(Map<String, String> entry) {
                            return new Income(entry.get("name"),entry.get("amount"),entry.get("frequency"));
                        }
                    }));
                }
    
                @Override
                public Locale locale() {
                    return Locale.ENGLISH;
                }
    
            }
    

    UPDATED Imports... Not all are required, keep what is relevant

    import cucumber.api.TypeRegistry;
    import cucumber.api.TypeRegistryConfigurer;
    import io.cucumber.cucumberexpressions.ParameterType;
    import io.cucumber.datatable.DataTable;
    import io.cucumber.datatable.DataTableType;
    import io.cucumber.datatable.TableCellTransformer;
    import io.cucumber.datatable.TableEntryTransformer;
    import io.cucumber.datatable.TableRowTransformer;
    import io.cucumber.datatable.TableTransformer;
    
    0 讨论(0)
  • 2020-12-18 05:40

    Use DataTableType annotation

    @DataTableType
    public Income incomeEntry(Map<String, String> entry) {
        return new Income(entry.get("name"), entry.get("amount"), entry.get("frequency"));
    }
    

    Then you can directly use the list of the custom class in step definition

    @When("^I Enter My Regular Income Sources$")
    public void I_Enter_My_Regular_Income_Sources(List<Income> incomes) throws Throwable {
        // More code    
    }
    
    0 讨论(0)
提交回复
热议问题