Alex Man
Alex Man

Reputation: 4886

Differentiate the cucmber step definition based on argument type

I have two cucumber step definition like as shown below

Map<String, Object> values = new HashMap<>();
:
:
@And("^(.+) has a value (false|true)$")
public void addValues(String key, Boolean value) {
    values.put(key, value);
}

@And("^(.+) has a value (.+)$")
public void addValues(String key, String value) {
    values.put(key, value);
}

In the first step definition I want to set the value as boolean and in the second definition the value as String but some how I'm getting io.cucumber.core.runner.AmbiguousStepDefinitionsException

for the given statements

Given name has a value Manu
And isMajor has a value true
:
:

Can someone please tell me how to differentiate the step definition with the same statement but with different argument type

My cucumber version is 6.8.1

Upvotes: 0

Views: 339

Answers (2)

Alexey R.
Alexey R.

Reputation: 8676

Change this:

^(.+) has a value (.+)$

to this:

^(.+) has a value ((?!true|false).+)$

This will eliminate ambiguity.

P.S. - After the change you entire class would look like:

public class ClassicStepDefs {

    Map<String, Object> values = new HashMap<>();

    @And("^(.+) has a value ((?!true|false).+)$")
    public void addValues(String key, String value) {
        values.put(key, value);
    }

    @And("^(.+) has a value (false|true)$")
    public void addValues(String key, Boolean value) {
        values.put(key, value);
    }

}

Upvotes: 1

M.P. Korstanje
M.P. Korstanje

Reputation: 12029

The step isMajor has a value true matches both regular expressions. So Cucumber can't figure out which method to call. If you make the other regular expression match any argument except true/false your step definitions are no longer ambiguous.

You can probably do that with a negative lookahead but I'll leave that as exercise for some one else.

Upvotes: 0

Related Questions