首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么IDE II看不到黄瓜的自定义注册参数?

为什么IDE II看不到黄瓜的自定义注册参数?
EN

Stack Overflow用户
提问于 2021-08-01 13:17:22
回答 1查看 91关注 0票数 0

https://github.com/aleks70694/cucumberStudy尝试启动流道时出现以下错误:

代码语言:javascript
复制
io.cucumber.core.exception.CucumberException: Could not create a cucumber expression for 'передадим в метод дату {localdate}'.
It appears you did not register a parameter type.

实现步骤:

代码语言:javascript
复制
    @Given("передадим в метод дату {localdate}")
    public void inMethodDate(LocalDate localdate) {
        System.out.println(localdate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
    }

RunTest.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
                }
        ));
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-02 21:31:47

如果你正在学习黄瓜,可以考虑使用starting with a more recent version。然后,可以更优雅地声明参数类型:

https://github.com/cucumber/cucumber-jvm/tree/main/java#parameter-type

代码语言:javascript
复制
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) {

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68610789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档