Java 黄瓜中的日期对象

Java 黄瓜中的日期对象,java,cucumber,gherkin,Java,Cucumber,Gherkin,我有一只像这样的黄瓜 Given the date of <date> When blah blah Then x y and z Examples: |2015-01-01| |2045-01-01| 给定的日期 什么时候胡说八道 然后是xy和z 示例: |2015-01-01| |2045-01-01| 当我从中生成stepdefs时,我得到@Given(“^date of(\\d+)-(\\d+)-(\\d+)-(\\d+$”) 该方法以三个整数作为参数生成。 我如何告诉C

我有一只像这样的黄瓜

Given the date of <date>
When blah blah
Then x y and z
Examples:
|2015-01-01|
|2045-01-01|
给定的日期
什么时候胡说八道
然后是xy和z
示例:
|2015-01-01|
|2045-01-01|
当我从中生成stepdefs时,我得到
@Given(“^date of(\\d+)-(\\d+)-(\\d+)-(\\d+$”)
该方法以三个整数作为参数生成。
我如何告诉Cucumber将其视为Java.Time LocalDate?有没有办法创建Cucumber会理解的映射器?或者至少,有没有一种方法可以将该日期对象视为字符串而不是三个数字?

修改步骤定义,以包含整个日期的字符串。可能使用类似于(.*)的内容,而不是3个整数

@Given("^the date of (.*?)$")
public void storeDate(@Transform(DateMapper.class) LocalDate date){

}
变压器类

public class DateMapper extends Transformer<LocalDate>{

    @Override
    public LocalDate transform(String date) {

        //Not too sure about the date pattern though, check it out if it gives correct result
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        return LocalDate.parse(date, formatter); 
   }

}
公共类DateMapper扩展了Transformer{
@凌驾
公共LocalDate转换(字符串日期){
//虽然对日期模式不太确定,但请检查它是否给出正确的结果
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“yyyy-MM-dd”);
返回LocalDate.parse(日期,格式化程序);
}
}

Cucumber应该为您将字符串格式转换为日期对象

请检查这是否有效-

Scenario Outline: Date  
Given the date of <date>
When blah blah
Then x y and z
Examples:
|date      |
|2015-01-01|
|2045-01-01|
这会像这样打印代码-

@Given("^the date of (.*?)$")
public void the_date_of(String strDate) throws Throwable {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    Date date = format.parse(strDate);
    System.out.println(date);
}
Thu Jan 01 00:00:00 AEDT 2015
Sun Jan 01 00:00:00 AEDT 2045

我仍然得到@Given(“^date of(\\d+)-(\\d+)-(\\d+)-(\\d+)$”),该方法是以三个整数作为参数生成的。如何告诉Cucumber处理错误您使用的是Cucumber生成的默认方法stepdef模式和参数。将其编辑为要将数据输入方法的字符串模式和参数。仍然会获取@Given(“^date of(\\d+)-(\\d+)-(\\d+)-(\\d+)$”),并使用三个整数作为参数生成方法。我怎样才能告诉Cucumber处理错误