Search code examples
javacucumberspring-boot-test

How to use parameter type instead regex in Cucumber for Java


I Have this Scenario:

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    |

And this is the generated Step definition:

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) -> {
        
        }
        
    }
}

How can I remove the regex and use a parameter type instead?

Like this:

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

To this:

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

PS: The error when I try is: "Undefined step reference"

Edit:

This is the full 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$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}

This is the IntelliJ Message Error:

enter image description here

These are my cucumber libraries (all from info.cukes groupId):

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

Solution

  • You are using Cucumber v1.2.5. Cucumber expressions were introduced in v3.0.0.