无法解析黄瓜4.x.x的JacksonTableTransformer

时间:2019-06-27 03:03:18

标签: datatable cucumber cucumber-jvm

this example之后,我正在使用JacksonTableTransformer将Cucumber 3.x.x数据表配置程序转换为Cucumber 4.2.0,但表示无法将其解析为一种类型。

功能

And I Enter My Regular Income Sources
    | name      | Salary        |
    | amount    | 25000         |
    | frequency | every 2 weeks |
And I Enter My Regular Expenses
    | name        | amount | frequency     |
    | Electricity | 5500   | Monthly       |
    | Water       | 900    | Weekly        |
    | Internet    | 1900   | Every 2 Weeks |
    | Cable TV    | 555    | Daily         |

配置器

import io.cucumber.datatable.dependency.com.fasterxml.jackson.databind.ObjectMapper;

public class Configurer implements TypeRegistryConfigurer {

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

    @Override
    public void configureTypeRegistry(TypeRegistry registry) {

        // Just added these 2 lines for Cucumber 4.2.0
        JacksonTableTransformer jacksonTableTransformer = new JacksonTableTransformer();
        registry.setDefaultDataTableEntryTransformer(jacksonTableTransformer);

        /*
         * Maps DataTable with header row to multiple objects of Type<T>. Each row below
         * the header is an object.
         */
        registry.defineDataTableType(new DataTableType(Transaction.class, new TableEntryTransformer<Transaction>() {
            @Override
            public Transaction transform(Map<String, String> entry) {
                return new Transaction(entry);
            }
        }));

        /*
         * Maps DataTable with label column to a single object of Type<T>. Left column
         * is field name, right column is value.
         */
        registry.defineDataTableType(new DataTableType(Transaction.class, new TableTransformer<Transaction>() {
            @Override
            public Transaction transform(DataTable table) throws Throwable {
                return new Transaction(table.asMaps().get(0));
            }
        }));

    }

}

enter image description here

我只有2个数据表,如果我删除添加的2行,则配置器适用于3.x.x和4.x.x。我确实想使用对象映射器。

2 个答案:

答案 0 :(得分:0)

  

在此示例之后,我正在使用JacksonTableTransformer将Cucumber 3.x.x数据表配置程序转换为Cucumber 4.2.0,但表示无法将其解析为类型。

如果您阅读链接的完整课程文件,则会发现JacksonTableTransformer is defined at line 99。即您将必须自行定义此类。

答案 1 :(得分:0)

这有效,但不确定如何做到。如果任何人都可以添加一线评论或删除说明,那么非常适合所有人参考。谢谢!

类型

public class Transaction {

    private String name = null;
    private String amount = null;
    private String frequency = null;
    private String month = null;

    public Transaction() {
        // Empty constructor
    }

    public Transaction(Map<String, String> entry) {
        this.name = entry.get("name");
        this.amount = entry.get("amount");
        this.frequency = entry.get("frequency");
        this.month = entry.get("month");
    }

    // getters and setters
}

功能

And I Enter My Regular Income Sources
    | name      | Salary        |
    | amount    | 25000         |
    | frequency | every 2 weeks |
And I Enter My Regular Expenses
    | name        | amount | frequency     |
    | Electricity | 5500   | Monthly       |
    | Water       | 900    | Weekly        |
    | Internet    | 1900   | Every 2 Weeks |
    | Cable TV    | 555    | Daily         |

StepDef

@When("I Enter My Regular Income Sources")
public void I_Enter_My_Regular_Income_Sources(@Transpose Transaction transaction) throws Throwable {
    // vertical datatable transforms to a single object
    // column 1 = field name, column 2 = value
}

@When("I Enter My Regular Expenses")
public void I_Enter_My_Regular_Expenses(DataTable table) throws Throwable {
    // datatable with header row transforms to a List of objects
    // 1 row = 1 object
    List<Transaction> transactions = table.asList(Transaction.class);   
}

配置器

public class Configurer implements TypeRegistryConfigurer {

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

    @Override
    public void configureTypeRegistry(TypeRegistry registry) {

        JacksonTableTransformer jtt = new JacksonTableTransformer();
        registry.setDefaultParameterTransformer(jtt);
        registry.setDefaultDataTableCellTransformer(jtt);
        registry.setDefaultDataTableEntryTransformer(jtt);

        /*
         * Maps DataTable with label column to a single object of Type<T>. Left column
         * is field name, right column is value.
         */
        registry.defineDataTableType(new DataTableType(Transaction.class, new TableTransformer<Transaction>() {
            @Override
            public Transaction transform(DataTable table) throws Throwable {
                return new Transaction(table.asMaps().get(0));
            }
        }));

    }

    private static final class JacksonTableTransformer
            implements ParameterByTypeTransformer, TableCellByTypeTransformer, TableEntryByTypeTransformer {

        private final ObjectMapper objMapper = new ObjectMapper();

        @Override
        public Object transform(String fromValue, Type toValueType) throws Throwable {
            return objMapper.convertValue(fromValue, objMapper.constructType(toValueType));
        }

        @Override
        public <T> T transform(String value, Class<T> cellType) throws Throwable {
            return objMapper.convertValue(value, cellType);
        }

        @Override
        public <T> T transform(Map<String, String> entry, Class<T> type, TableCellByTypeTransformer cellTransformer)
                throws Throwable {
            return objMapper.convertValue(entry, type);
        }

    }

}