如何在 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) -> {
        
        }
        
    }
}

如何删除正则表达式并使用参数类型代替?

像这样:

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

为此:

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

PS:我尝试时的错误是:“未定义的步骤引用”

编辑:

这是完整的堆栈跟踪:

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$2.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 消息错误

enter image description here

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

  1. cucumber-java8 (1.2.5)
  2. 黄瓜弹簧 (1.2.5)
  3. cucumber-junit (1.2.4)

解决方法

您使用的是 Cucumber v1.2.5。 Cucumber expressions were introduced in v3.0.0