简体   繁体   中英

cucumber jvm varargs support in step definition

How to use power of varargs while defining step definitions in cucumber java bindings. I have below step

Given I have following product: prod1, prod2, prod3

My Step definition

@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
}

I know workaround can be to except the complete string after colon and then in the code break the string using split(",") and dump it into array. But i just want to know if cucumber on its own supports varargs pattern.

TIA!!!

I dont know if varargs are supported in cucumber, but maybe you can archieve your goal with direct list matchings?

You can define Example Lists in the Feature files in Cucumber

You can define them at the end of a Step:

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

Or inline:

@Given i want a list: Entry1, Entry2

Then you can have glue code like:

@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
}

you can find more info here: https://cukes.info/docs/reference/jvm#step-definitions

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM