如何在黄瓜中使用可选参数

时间:2016-09-14 16:35:27

标签: java cucumber gherkin

我想要相同的Gherkin句子(有参数和没有参数):

Gherkin与参数:

When a 'notify' message is sent to the green box with the properties.
 |type|message|
 |error|The error message|

没有参数的小黄瓜:

When a 'notify' message is sent to the green box with the properties.

Java(黄瓜):

@When("^a '(.*)' message is sent to the green box with the properties.$")
public void hello(String name, List<GherkinCondition> conditions) {
    ...
}

我有一个错误,因为java方法是用2个参数声明的,在“没有参数”的情况下,我只有一个。

堆栈跟踪:

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

5 个答案:

答案 0 :(得分:3)

黄瓜步骤定义不支持可选参数。

您可以编写两个不同的步骤定义,也可以为第二个案例提供一个空的数据表。

When a 'notify' message is sent to the green box with the properties.
 |type|message|

甚至

When a 'notify' message is sent to the green box with the properties.
     |||

答案 1 :(得分:0)

我创建了一个PR来解决此问题:https://github.com/cucumber/cucumber-jvm/pull/1056

小黄瓜脚本测试:

@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黄瓜代码:

    @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 + "!");
    }

答案 2 :(得分:0)

这对我有用。我可以通过IND或USA。注意:如果您没有通过任何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); 
}

答案 3 :(得分:0)

我得到了问题的答案: -

@When(&#34; ^我在(IND | USA)搜索(。*)?数据库$&#34;)

我正在寻找美国或IND的可选参数。以上行解决我的问题。 (和|

之后需要空格

答案 4 :(得分:0)

我正在这样做:

@Then("^Something is (not )?counted$")
public void somethingCounted(String optional) {
    //
}

正则表达式同时匹配“已计算在内”和“未计算在内”

对于第一种情况,您将获得“ not”值,对于第二种情况,您将获得null。

相关问题