黄瓜没有将日期字符串从功能文件中的数据表序列化到我的pojo中的LocalDate字段

use*_*898 1 java xstream cucumber cucumber-jvm cucumber-java

我试图找出如何在我的步骤定义中从黄瓜功能文件中解析日期字段。

class Person{ 
  String name
  LocalDate dob
}

scenario: do something with people
Given  list of people:
                     |name|dob|
                     | john| 20-09-2001|

@Given("^list of people:")
public void doSomething(List<Person> people) {

}
Run Code Online (Sandbox Code Playgroud)

请注意,我无权访问Person类,我确定自己必须编写自己的转换器或注册某个库中某人编写的转换器,在搜索到的唯一选项后,我看到的是使用@Transform更改它们pojo在java.time.LocalDate字段上。

我目前收到以下异常

cucumber.runtime.CucumberException:              cucumber.deps.com.thoughtworks.xstream.converters.ConversionException: Cannot deserialize object with new readObject()/writeObject() methods
---- Debugging information ----
class               : java.time.LocalDate
required-type       : java.time.LocalDate
converter-type      : cucumber.deps.com.thoughtworks.xstream.converters.reflection.SerializableConverter
path                : /list/com.pkg.Person/dob
Run Code Online (Sandbox Code Playgroud)

我尝试将dateformat更改为yyyy-MM-dd,通常可以,但在这种情况下不行。我将不胜感激关于如何设置和注册自定义转换器的任何指示

我的黄瓜依赖项如下,如果有任何区别,我可以将其替换为较新版本。

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>2.4.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>2.4.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

Gra*_*per 5

黄瓜不支持新的Java日期时间类的自动转换。但是它确实支持转换为旧的util Date类,该类没有用,因为您没有对dataobject的写入权限。

为此,您将需要@XStreamConverters在测试运行器上使用注释。这会将自定义xstream转换器添加到黄瓜中的即用型转换器中。

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { ""}, tags = { "" }, glue = "stepdefs",
        features = "" ) --> use your own values
@XStreamConverters(@XStreamConverter(LocalDateCon.class))
public class RunSampleTest {
Run Code Online (Sandbox Code Playgroud)

现在,转换器类将字符串解析为LocalDate并创建数据对象的集合。这将支持“ 15-05-2016”之类的日期。只需DEFAULT_DATE_PATTERN根据您的日期格式更改即可。

public class LocalDateCon implements Converter{

    public boolean canConvert(Class type) {
        //return type.equals(LocalDate.class);
        return LocalDate.class.isAssignableFrom(type);
    }

    private static final String            DEFAULT_DATE_PATTERN = "dd-MM-yyyy";
    private static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_PATTERN);

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        LocalDate  date = (LocalDate) value;
        String result = date.format(DEFAULT_DATE_FORMATTER);
        writer.setValue(result);
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        LocalDate result = LocalDate.parse(reader.getValue(), DEFAULT_DATE_FORMATTER);
        return result;
    }

}
Run Code Online (Sandbox Code Playgroud)

另外,您将不能完全使用@Transform注释,因为注释仅适用于单个值而不适用于集合。我干净利落地使用了,因为需要在转换器代码中创建对象,这很麻烦。