向黄瓜数据表注册LocalDateTime xstream转换器 [英] Register LocalDateTime xstream transformer with cucumber datatable
问题描述
我在Gherkin文件中有以下数据表:
鉴于系统知道以下设备:
|序列号| CreationDate |
| 1234A | 2016-05-17T08:41:17.970Z |
| 5678A | 2012-03-16T08:21:17.970Z |
以及以下Java中的匹配步骤定义:
@Given( ^系统知道以下设备:$)
public void SystemKnowsAboutTheFollowingEquipments(List< Equipment>设备)引发Throwable {
/ /步骤代码...
}
使用POJO Equipment.java:
公共类设备{
private String equipmentId;
私人LocalDateTime creationDate;
}
由于默认情况下黄瓜不支持LocalDateTime转换,所以我会希望注册我的自定义xstream变形器,以便在我的设备POJO中正确处理LocalDateTime。
假设您可以访问设备POJO的代码。
可以使用 @XStreamConverter
批注注册 XStream
转换器类。
-
数据表中的列名称和所需POJO中的字段需要匹配,以便Cucumber能够将列匹配到各个字段。 p>
-
创建一个自定义的
DTZConverter
类,为该类扩展AbstractSingleValueConverter
DateTime解析逻辑。请修改下面使用的模式。
公共类DTZConverter扩展AbstractSingleValueConverter {
@Override
public boolean canConvert(Class type){
return type.equals(LocalDateTime.class);
}
@Override
公共对象fromString(String dtz){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( yyyy-MM-dd HH:mm);
返回LocalDateTime.parse(dtz,formatter);
}
}
- 用
@XStreamConverter(DTZConverter.class)
注释creationDate字段。
公共类设备{
private String equipmentId;
@XStreamConverter(DTZConverter.class)
私有LocalDateTime creationDate;
@Override
public String toString(){
return Equipment [equipmentId = + equipmentId +,creationDate =
+ creationDate + ];
}
}
I have the following datatable in a Gherkin file:
Given the system knows about the following equipments:
| SerialNumber | CreationDate |
| 1234A | 2016-05-17T08:41:17.970Z |
| 5678A | 2012-03-16T08:21:17.970Z |
And the following matching step definition in java :
@Given("^the system knows about the following equipments:$")
public void theSystemKnowsAboutTheFollowingEquipments(List<Equipment> equipments) throws Throwable {
// step code...
}
With the POJO Equipment.java :
public class Equipment {
private String equipmentId;
private LocalDateTime creationDate;
}
Since cucumber does not support LocalDateTime transformation by default, I would like to register my custom xstream Transformer in order for LocalDateTime to be handled correctly inside my Equipment POJO. Is there a way to do this?
Assuming that you have access to the code of the Equipment POJO.
You can use the @XStreamConverter
annotation to register your XStream
converter class.
The column names in the datatable and fields in the desired POJO need to match so that Cucumber can do its magic of matching columns to respective fields.
Create a custom
DTZConverter
class extendingAbstractSingleValueConverter
for the DateTime parsing logic. Do modify the pattern used below.
public class DTZConverter extends AbstractSingleValueConverter { @Override public boolean canConvert(Class type) { return type.equals(LocalDateTime.class); } @Override public Object fromString(String dtz) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); return LocalDateTime.parse(dtz, formatter); } }
- Annotate the creationDate field with
@XStreamConverter(DTZConverter.class)
.
public class Equipment { private String equipmentId; @XStreamConverter(DTZConverter.class) private LocalDateTime creationDate; @Override public String toString() { return "Equipment [equipmentId=" + equipmentId + ", creationDate=" + creationDate + "]"; } }
这篇关于向黄瓜数据表注册LocalDateTime xstream转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!