Search code examples
cucumberbddgherkincucumber-java

How to use Examples with only one variable in Gherkin Cucumber?


I am trying to write Gherkins with only one variable in a Scenario Outline. I see an error "1 out of bounds for length" when I try running my feature file. However if I add a second variable (another column and another variable) this will pass.

Is there a way to write Scenario Outlines with just one variable instead of two or more? If so, how?

Thank you!

Example A

Feature: Is this a valid fruit?

  Scenario Outline: Is this a fruit 
    When I ask whether <fruit> is a fruit
    Then I should see "Yes this is a fruit" message

  Examples:
    | fruit     |
    | Kiwi      | 
    | Apple     | 
    | Pineapple | 

Gives me an error "1 out of bounds for length"

I can change it to the below format and it passes, but I want to avoid adding an entire column with the same string just to make the Cucumber Gherkins pass.

Example B

Feature: Is this a valid fruit?

  Scenario Outline: Is this a fruit 
    When I ask whether <fruit> is a fruit
    Then I should see <message> message

  Examples:
    | fruit     | message 
    | Kiwi      | "Yes this is a fruit"
    | Apple     | "Yes this is a fruit"
    | Pineapple | "Yes this is a fruit"

How can we make Example A valid and pass? Thanks!


Solution

  • This is what works for me with Cucumber 7.4.1:

    package so;
    
    import io.cucumber.java.en.Then;
    import io.cucumber.java.en.When;
    
    public class StepDef {
        @When("I ask whether {word} is a fruit")
        public void i_ask_whether_kiwi_is_a_fruit(String value) {
            System.out.println(value);
        }
    
        @Then("I should see {string} message")
        public void i_should_see_message(String string) {
            System.out.println(string);
        }
    }
    
    

    Which with this feature file:

    Feature: Is this a valid fruit?
    
      Scenario Outline: Is this a fruit
        When I ask whether <fruit> is a fruit
        Then I should see "Yes this is a fruit" message
    
      Examples:
        | fruit     |
        | Kiwi      |
        | Apple     |
        | Pineapple |
    

    gives:

    Kiwi
    Yes this is a fruit
    Apple
    Yes this is a fruit
    Pineapple
    Yes this is a fruit