gpt4 book ai didi

java - cucumber 中的日期对象

转载 作者:行者123 更新时间:2023-11-29 06:53:32 25 4
gpt4 key购买 nike

我有一个像这样的 cucumber stepdef

Given the date of <date>
When blah blah
Then x y and z
Examples:
|2015-01-01|
|2045-01-01|

当我从中生成 stepdefs 时,我得到 @Given("^the date of (\\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);
}

}

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

关于java - cucumber 中的日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com