cucumber jvm varargs support in step definition
Asked Answered
C

2

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

Coeval answered 7/3, 2015 at 17:29 Comment(0)
F
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

Faddish answered 7/4, 2015 at 12:38 Comment(2)
thnx..but i wanted to know if varags is supported..i've already explored other alternativesCoeval
this seems to be not possible for now, but i dont know in which way varargs are more powerfull than collections?Faddish
L
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));
    }
}
Latoyialatreece answered 14/12, 2018 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.