Search code examples
intellij-ideacucumbercucumber-java

IntelliJ asking to choose declaration between Cucumber steps that are not the same


I've two steps that have some similarities but not completely phrased the same way. However, IntelliJ keeps asking me to choose between them as if they were duplicates and not sure why?

enter image description here

@And("^I add \"([^\"]*)\" as the history|comment|details for the item$")
public void iAddAsTheHistoryForTheItem(String text) {
    addComment(text);
}


@Then("^I am able to add a history|comment|details to the item$")
public void iAmAbleToAddAHistoryToTheItem() {
    addHistory();
    assertFalse(isHistoryClean());
}

Thank you.


Solution

  • It's because of the OR symbol in the step definition ( | ).

    In your case it matches both step definitions because of the word 'comment'

    It's like you say to the IDE: look for 'I am able to add a history' OR look for 'comment' OR look for 'details to the item' (for @Then("^I am able to add a history|comment|details to the item$")).

    For the step definition @And("^I add \"([^\"])\" as the history|comment|details for the item$") you have -> look for I add \"([^\"])\" as the history OR look for 'comment' OR look for 'details for the item'

    I see 2 possible solutions here:

    1) You could try to refactor the step definitions this way in order to avoid the current situation:

    @Then("^I am able to add a (history|comment|details) to the item$")

    @And("^I add \"([^\"]*)\" as the (history|comment|details) for the item$")

    OR

    2) You could use a parameter in gherkin, this way you could provide there any value and it will not suggest you both step definition

    Ex:

    Then I am able to add a "put your value here" to the item

    and the corresponding step definition will be

     @Then("^I am able to add a \"([^\"]*)\" to the item$")
    public void iAmAbleToAddAToTheItem(String arg0) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    

    (and make a similar modification for the second step)