https://github.com/aleks70694/cucumberStudy尝试启动流道时出现以下错误:
io.cucumber.core.exception.CucumberException: Could not create a cucumber expression for 'передадим в метод дату {localdate}'.
It appears you did not register a parameter type.实现步骤:
@Given("передадим в метод дату {localdate}")
public void inMethodDate(LocalDate localdate) {
System.out.println(localdate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}RunTest.java
package myStudy.runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
glue = {"myStudy/stepDefinitions", "myStudy/typeRegistry"},
features = "src/test/resources/features"
)
public class RunTest extends AbstractTestNGCucumberTests {
}TypeRegistryConfiguration.java路径: src/test/java/myStudy/typeRegistry/typeRegistryConfigurer.java
package myStudy.typeRegistry;
import io.cucumber.core.api.TypeRegistry;
import io.cucumber.core.api.TypeRegistryConfigurer;
import io.cucumber.cucumberexpressions.ParameterType;
import io.cucumber.cucumberexpressions.Transformer;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
class TypeRegistryConfiguration implements TypeRegistryConfigurer {
@Override
public Locale locale() {
// требуется только для определения формата разделителя в float и double
return new Locale("ru");
}
@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
// добавление в реестр определения необходимого типа
typeRegistry.defineParameterType(new ParameterType<>(
// название параметра, используемое в определении шага:
"localdate",
// регулярка, для поиска необходимого значения в фиче:
"[0-9]{2}.[0-9]{2}.[0-9]{4}",
// тип параметра:
LocalDate.class,
// функция, преобразующая входящую строку к нужному типу
(Transformer<LocalDate>) s -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
return LocalDate.parse(s, formatter);
}
));
}
}发布于 2021-08-02 21:31:47
如果你正在学习黄瓜,可以考虑使用starting with a more recent version。然后,可以更优雅地声明参数类型:
https://github.com/cucumber/cucumber-jvm/tree/main/java#parameter-type
package com.example.app;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.Given;
import java.time.LocalDate;
public class StepDefinitions {
@ParameterType("([0-9]{4})-([0-9]{2})-([0-9]{2})")
public LocalDate iso8601Date(String year, String month, String day) {
return LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
}
@Given("today is {iso8601Date}")
public void today_is(LocalDate date) {
}
}https://stackoverflow.com/questions/68610789
复制相似问题