Search code examples
gherkincucumber-java

Cucumber (Gherkin): How to use parameters of type non string (Long) in test steps?


I'm trying to write some Cucumber tests in order to get familiarized with the Gherkin language. Here is a feature sample:

  Scenario: Get a customer
    When an user wants to get a customer <id>
    Then an HTTP 200 status together with the requested customer data is returned

And here is the associated step:

@When("an user wants to get a customer {long}")
public void anUserWantsToGetACustomer(Long id)
{
  ...
}

Running the verify goal raises the following exception:

[ERROR] Get a customer  Time elapsed: 0.023 s  <<< ERROR!
io.cucumber.junit.UndefinedStepException: 
The step "an user wants to get a customer <long>" is undefined. You can implement it using the snippet(s) below:

@When("an user wants to get a customer <id>")
public void anUserWantsToGetACustomer_id() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

Obviously, it doesn't understand that the step method need to take a parameter of type Long. However, for parameter of type String it works, for example:

Scenario: Get a customer
  When an user wants to get a customer "<id>"
  Then an HTTP 200 status together with the requested customer data is returned
  ...
  @When("an user wants to get a customer {string}")
  public void anUserWantsToGetACustomer(String id)
  {
    ...
  }

So, what kind of syntax should I use here in order that my tests accepts a Long parameter ?

Kind regards,

Seymour


Solution

  • You can use {long} in your step defs

    Scenario: test long type
         Given I log the type of 41
    
    @Given("I log the type of {long}")
            public void logType(Long testLong ) {
                System.out.println("type of 41 is:"+ testLong.getClass());
           }
    

    prints

    type of 41 is:class java.lang.Long
    

    This also works

     Scenario Outline: test long type
         Given I log the type of <id> 
      
          Examples:
            |id|
            |  41  |
    
      @Given("I log the type of {long}")
            public void logType(Long testLong ) {
                System.out.println("type of " +testLong +" is:"+ testLong.getClass());
           }