Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how use optional parameters in cucumber

I want the same Gherkin sentence (with paramaters and without paramaters):

Gherkin with paramaters:

When a 'notify' message is sent to the green box with the properties.
 |type|message|
 |error|The error message|

Gherkin without paramaters:

When a 'notify' message is sent to the green box with the properties.

Java (cucumber):

@When("^a '(.*)' message is sent to the green box with the properties.$")
public void hello(String name, List<GherkinCondition> conditions) {
    ...
}

I have a error because java method is is declared with 2 parameters and in the case "without paramaters" I have only one.

stack trace:

cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'steps.CommonSteps.hello(String,GherkinCondition>) in file:/C:/workspace/xxxxx/java/target/classes/' with pattern [^a '(.*)' message is sent to the green box with the properties.$] is declared with 2 parameters. However, the gherkin step has 1 arguments [notify]. 
like image 849
Stéphane GRILLON Avatar asked Aug 30 '25 17:08

Stéphane GRILLON


2 Answers

Cucumber step definitions do not support optional parameters.

Either you write two different step definitions or you can give an empty datatable for the second case.

When a 'notify' message is sent to the green box with the properties.
 |type|message|

or even

When a 'notify' message is sent to the green box with the properties.
     |||
like image 84
Grasshopper Avatar answered Sep 02 '25 07:09

Grasshopper


I`m doing it this way:

@Then("^Something is (not )?counted$")
public void somethingCounted(String optional) {
    //
}

Regex matches both "Something is counted" and "Something is not counted"

For the first case, you'll get "not" value, for the second case you`ll get null.

like image 40
Robert Van-Vas Avatar answered Sep 02 '25 08:09

Robert Van-Vas