Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 983
Mrunal Gosar Avatar asked Mar 07 '15 17:03

Mrunal Gosar


People also ask

Which Cucumber option can be used to give path for step definition files?

An annotation followed by the pattern is used to link the Step Definition to all the matching Steps, and the code is what Cucumber will execute when it sees a Gherkin Step. Cucumber finds the Step Definition file with the help of the Glue code in Cucumber Options.

Why step definition is not recognized in Cucumber?

If Cucumber is telling you that your steps are undefined, when you have defined step definitions, this means that Cucumber cannot find your step definitions. You'll need to make sure to specify the path to your step definitions (glue path) correctly.

How do you write a step definition in Cucumber Java?

A Step Definition is a Java method Kotlin function Scala function JavaScript function Ruby block with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.


1 Answers

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

like image 72
Dude Avatar answered Sep 30 '22 13:09

Dude