'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]. 


Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

This works for me. I can pass either IND or USA. Note: this would not work if you don't pass any IND,USA.

@When("^I search (.*) (IND|USA)? in Database$")
public void searchInDatabase(String name, String country){
    if(country.equals("IND"))
        dbObj.searchDatabase(name, true);
    else if(country.equals("USA"))
        dbObj.searchDatabase(name, false);
    else
        dbObj.searchDatabase(name, true); 
}

Solution 4:[4]

I got the ans of my problem:-

@When("^I search (.*) in( IND| USA)? Database$")

I was looking for optional parameter either USA or IND. above line fix my problem. spaces are needed after ( and |

Solution 5:[5]

Can't say how to solve this problem specifically in Java, but when using PHP I think you can achieve what you aim at by setting your param to NULL, i.e.:

Imagine you want to check colors, and your Gherkin states something like:

Given I want to check colors "red" and "green"

You'd have a function such as this one:

public function iCheckColors($arg1, $arg2) {
    // do stuff here;
}

Then you might want to check three colors in your Gherkin line, such as:

Given I want to check colors "red" and "green" and "blue"

In this case, you'd need to add this third param to your function:

public function iCheckColors($arg1, $arg2, $arg3) {
    // do stuff here;
}

The problem here is, if you were to use the first Gherkin line again, your function would throw an error, since she expects the third param, but if you set this param to NULL:

public function iCheckColors($arg1, $arg2, $arg3 = NULL) {
    // do stuff here;
}

Then both Gherkin lines added to the function should work. It still expects two params and, in case you'll add a third param to your Gherkin line, it'd override the NULL value.

Hope this is what you're trying to achieve.

Solution 6:[6]

Till now, there is no solution for this;

Alternative text only works when there is no whitespace between the alternative parts.

Hence your way of using () -> optional is the best we have now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grasshopper
Solution 2 Robert Van-Vas
Solution 3
Solution 4 Kripal Singh Thakur
Solution 5 VĂ­ctor
Solution 6 Yub Raj