5

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!!!

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71

2 Answers2

13

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

Dude
  • 692
  • 1
  • 4
  • 25
  • thnx..but i wanted to know if varags is supported..i've already explored other alternatives – Mrunal Gosar Apr 08 '15 at 12:01
  • this seems to be not possible for now, but i dont know in which way varargs are more powerfull than collections? – Dude Apr 14 '15 at 08:00
0
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));
    }
}
Avinash Pande
  • 1,510
  • 19
  • 17