我的故事步骤是这样的:
When the user selects '<past>' date in field start date
When the user selects '<future>' date in field start date
步骤java文件中对应的方法是
@When("the user selects '$datevalue' date in field $field")
public void selectDate(Date dateValue, String fieldName) {
...
}
自定义日期转换器
parameterConverters.addConverters(new ParameterConverters.DateConverter() {
@Override
public Object convertValue(String value, Type type) {
if (value.equals("<future>")){
Calendar cal = GregorianCalendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 1);
return cal.getTime();
} else if (value.equals("<past>")){
Calendar cal = GregorianCalendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -1);
return cal.getTime();
}
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(value);
} catch (ParseException e) {
throw new ParameterConvertionFailed("Failed to convert value "
+ value + " with date format yyyy-MM-dd", e);
}
}
@Override
public boolean accept(Type type) {
if (type instanceof Class<?>) {
return String.class.isAssignableFrom((Class<?>) type);
}
return false;
}
});
但是自定义的日期转换器没有效果,在我看来默认实现总是执行,因为我收到错误 org.jbehave.core.steps.ParameterConverters$ParameterConvertionFailed: Failed to convert value with date format dd/MM/yyyy
任何想法?