Search code examples
javacucumbergherkin

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

  • I create a PR for resolve this problem: https://github.com/cucumber/cucumber-jvm/pull/1056

    Gherkin script test:

    @hello 
    Feature: hello (Function to validate the environment.) 
    
        Scenario Outline: Function to validate the environment.
            Given me a hello, please. Best Regards '<author>'.
            Given me a hello, please. Best Regards '<author>'.
                |key|expected|actual|
            Given me a hello, please. Best Regards '<author>'.
                |key|expected|actual|
                |zip|35000|<zip>|
                |city|Rennes|<city>|
    
            Given me a bye, please. Best Regards '<author>'.
            Given me a bye, please. Best Regards '<author>'.
                |zip|<zip>|
                |city|<city>|
    
            Given me a cat, please. Best Regards '<author>'.
    
        Examples:
        #Begin Examples#
        |author|zip|city|
        |Jenkins Q1|35000|Rennes|
        |Jenkins Q2|35000|Rennes|
        #End Examples#
    

    Java Cucumber code:

        @Given("^me a hello, please. Best Regards '(.*)'.$")
        public void hello(String name, List<GherkinCondition> conditions) {
            int i = 0;
            for (GherkinCondition gherkinCondition : conditions) {
                i++;
                logger.info(String.format("  expected contition N°%d=%s", i, gherkinCondition.getExpected()));
                logger.info(String.format("  actual   contition N°%d=%s", i, gherkinCondition.getActual()));
            }
            logger.info("Hello " + name + "!");
        }
    
        @Given("^me a cat, please. Best Regards '(.*)'.$")
        public void hello(String name) {
            logger.info("Take my cat " + name + "!");
        }
    
        @Given("^me a bye, please. Best Regards '(.*)'.$")
        public void bye(String name, Map<String, String> params) {
            int i = 0;
            for (Map.Entry<String, String> entry : params.entrySet()) {
                i++;
                logger.info(String.format("  Key N°%d: %s   Value:%s", i, entry.getKey(), entry.getValue()));
            }
            logger.info("Bye " + name + "!");
        }