如何在 Java 的 Cucumber 中使用参数类型代替正则表达式

How to use parameter type instead regex in Cucumber for Java

我有这个场景:

Scenario Outline: Client makes call to GET /entity with pagination
    When client calls /entity with <page> and <perPage>
    Then client receives status code of <status>

Examples:
  | page | perPage | status |
  | 1    | 5       | 200    |

这是生成的步骤定义:

import cucumber.api.java8.En;

@ContextConfiguration
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EntityApiStepdefs implements En {
  
    public EntityApiStepdefs() {
    
        When("^client calls /entity with ([^\"]*) and ([^\"]*)$",
                    (Integer page, Integer perPage) -> {
        
        }
        
    }
}

如何删除 regex 并改用 参数类型

像这样:

"^client calls /entity with ([^\"]*) and ([^\"]*)$"

为此:

"client calls /entity with {string} and {string}" 
// or:
"client calls /entity with {int} and {int}" 

PS:我尝试时的错误是:“Undefined step reference”

编辑:

这是完整的 Stacktrace:

Exception in thread "main" cucumber.runtime.CucumberException: Error creating bean with name 'com.test.app.entity.api.EntityApiStepdefs': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.app.entity.api.EntityApiStepdefs]: Constructor threw exception; nested exception is cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 27
    client calls /entity with {int} and {int}
                               ^
        at cucumber.runtime.java.spring.SpringFactory.getInstance(SpringFactory.java:182)
        at cucumber.runtime.java.JavaBackend.buildWorld(JavaBackend.java:131)
        at cucumber.runtime.Runtime.buildBackendWorlds(Runtime.java:140)
        at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:38)
        at cucumber.runtime.model.CucumberScenarioOutline.run(CucumberScenarioOutline.java:46)
        at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
        at cucumber.runtime.Runtime.run(Runtime.java:121)
        at cucumber.api.cli.Main.run(Main.java:36)
        at cucumber.api.cli.Main.main(Main.java:18)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.test.app.entity.api.EntityApiStepdefs': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.app.entity.api.EntityApiStepdefs]: Constructor threw exception; nested exception is cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 27
    client calls /entity with {int} and {int}
                               ^
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:345)
        at cucumber.runtime.java.spring.GlueCodeScope.get(GlueCodeScope.java:15)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1018)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
        at cucumber.runtime.java.spring.SpringFactory.getInstance(SpringFactory.java:180)
        ... 8 more
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.app.entity.api.EntityApiStepdefs]: Constructor threw exception; nested exception is cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 27
    client calls /entity with {int} and {int}

这是 IntelliJ 消息错误:

这些是我的黄瓜库(全部来自 info.cukes groupId):

  1. 黄瓜-java8 (1.2.5)
  2. 黄瓜-spring (1.2.5)
  3. 黄瓜-junit (1.2.4)

您正在使用 Cucumber v1.2.5。 Cucumber expressions were introduced in v3.0.0.