【问题标题】:cucumber jvm varargs support in step definition步骤定义中的黄瓜 jvm 可变参数支持
【发布时间】:2018-12-14 15:04:54
【问题描述】:

如何在 cucumber java 绑定中定义步骤定义时使用可变参数的强大功能。我有以下步骤

Given I have following product: prod1, prod2, prod3

我的步骤定义

@Given("^I have following product [(exhaustive list of products or pattern of the product names)]$")
public void stepDef(String...args)
{
//Process varargs array "args" in here
}

我知道解决方法可以是除了冒号后的完整字符串,然后在代码中使用 split(",") 打破字符串并将其转储到数组中。但我只想知道黄瓜本身是否支持可变参数模式。

TIA!!!

【问题讨论】:

    标签: java bdd cucumber-jvm cucumber-junit


    【解决方案1】:

    我不知道黄瓜是否支持可变参数,但也许你可以通过直接列表匹配来实现你的目标?

    您可以在 Cucumber 的功能文件中定义示例列表

    您可以在步骤结束时定义它们:

    @Given i want a list
    |Entry1|
    |Entry2|
    

    或内联:

    @Given i want a list: Entry1, Entry2
    

    然后你可以有如下胶水代码:

    @Given(^i want a list$)
    public void i_want_a_list(List<String> entries) {
    //do stuff
    }   
    
    @Given(^i want a list: (.*)$)
    public void i_want_a_list(List<String> entries){
     //Do something with the list
    }
    

    您可以在这里找到更多信息:https://cukes.info/docs/reference/jvm#step-definitions

    【讨论】:

    • thnx..但我想知道是否支持可变参数..我已经探索了其他替代方案
    • 目前这似乎是不可能的,但我不知道 varargs 在哪种方面比集合更强大?
    【解决方案2】:
    If your steps like below-
    Given I have following product
    |prod1|
    |prod2|
    |prod3|
    Then step definition becomes-
    
    @Given("^I have following product$")
    public void i_have_following_product(DataTable dt) throws Exception
    {
      List<List<String>> outerList = dt.rows();
      for(List<String> innerList : outerList)
        {
          System.out.println(innerLlist.get(0));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多