黄瓜(小黄瓜):如何在测试步骤中使用非字符串(Long)类型的参数?

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

我正在尝试编写一些 Cucumber 测试以熟悉 Gherkin 语言。 这是一个功能示例:

  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 {long}")
public void anUserWantsToGetACustomer(Long id)
{
  ...
}

运行 验证目标引发以下异常:

[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();
}

很明显,step方法需要带一个Long类型的参数,它不理解。但是,对于 String 类型的参数,它可以工作,例如:

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

那么,为了让我的测试接受 Long 参数,我应该在这里使用哪种语法?

亲切的问候,

西摩

您可以在步骤定义中使用 {long}

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());
       }

打印

type of 41 is:class java.lang.Long

这也有效

 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());
       }