Ste*_*eve 1 java cucumber gherkin
我有这样的黄瓜stepdef
Given the date of <date>
When blah blah
Then x y and z
Examples:
|2015-01-01|
|2045-01-01|
Run Code Online (Sandbox Code Playgroud)
当我从中生成stepdefs时,我得到@Given("^the date of (\\d+)-(\\d+)-(\\d+)$")
And该方法是使用三个整数作为参数生成的。如何告诉Cucumber将其视为Java.Time LocalDate?有没有办法创建一个Cucumber会理解的映射器?或者至少,有一种方法可以将该日期对象视为字符串而不是三个数字?
修改步骤定义以在整个日期中使用字符串。也许使用(。*?)之类的东西代替3个整数。
@Given("^the date of (.*?)$")
public void storeDate(@Transform(DateMapper.class) LocalDate date){
}
Run Code Online (Sandbox Code Playgroud)
变压器类
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);
}
}
Run Code Online (Sandbox Code Playgroud)
黄瓜应该为您将字符串格式转换为日期对象
在 Cucumber 7 中,我定义了一个新的@ParameterType:
@ParameterType("\\d{2}\\.\\d{2}\\.\\d{4}")
public LocalDate mydate(String dateString) {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd.MM.yyyy"));
}
Run Code Online (Sandbox Code Playgroud)
然后我可以使用步骤定义,@Given例如:
@Given("person has birthdate {mydate}")
public void person_birthdate(LocalDate birthDate) {
... // do something
}
Run Code Online (Sandbox Code Playgroud)
占位符名称{mydate}是映射方法的名称,但您可以通过 覆盖它@ParameterType.name。