Java Cucumber arity mismatch:尝试将多个匹配组绑定到单个 stepdef 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20447605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:00:33  来源:igfitidea点击:

Cucumber arity mismatch: trying to bind several matching groups to a single stepdef parameter

javaregexcucumbercucumber-jvm

提问by Elnur Abdurrakhimov

I'm trying to create a regular expression that will match the following:

我正在尝试创建一个匹配以下内容的正则表达式:

Given I'm a user
Given I am a user
Given Dylan is a user

And this is my step definition:

这是我的步骤定义:

@Given("^(?:(I)'m|(I) am|(.+) is) a user$")
public void aUserExists(String username) throws Throwable {
}

It's matching what I need, but seems like Cucumber is trying to assign each capturing group in the non-capturing group to a parameter, while I want it to assign just one:

它符合我的需要,但似乎 Cucumber 试图将非捕获组中的每个捕获组分配给一个参数,而我希望它只分配一个:

cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'hello.MyStepdefs.aUserExists(String) in [...] with pattern [^(?:(I)'m|(I) am|(.+) is) a user$] is declared with 1 parameters. However, the gherkin step has 3 arguments [I, null, null].

黄瓜.runtime.CucumberException: Arity mismatch: Step Definition 'hello.MyStepdefs.aUserExists(String) in [...] with pattern [^(?:(I)'m|(I) am|(.+) is)一个 user$] 用 1 个参数声明。但是,小黄瓜步骤有 3 个参数 [I, null, null]。

I'm coming from PHP background and this regular expression worked great with Behat. Maybe I'm not getting something about Java regular expressions or Cucumber behavior.

我来自 PHP 背景,这个正则表达式与Behat配合得很好。也许我对 Java 正则表达式或 Cucumber 行为一无所知。



Update

更新

Since all the current answers are having the same problem, I'm updating the question.

由于所有当前的答案都存在相同的问题,因此我正在更新问题。

I don't want to match a string like:

我不想匹配这样的字符串:

Given I is a user

I want to match Ionly when it's followed by 'mor am. I hope it's not an impossible thing to do with Java regular expressions and Cucumber.

我只想在I后跟'mor时匹配am。我希望用 Java 正则表达式和 Cucumber 做这件事不是不可能的。

采纳答案by Pshemo

I am not sure if that is what you are looking for but if you want to capture user name in group one without creating more groups then maybe try

我不确定这是否是您要查找的内容,但是如果您想在不创建更多组的情况下捕获第一组中的用户名,则可以尝试

"^(\w+)(?:(?<=I\b)(?:'m| am)|(?<!^I\b) is) a user$"

回答by pobrelkey

You could rewrite the regex to use only one capturing group, though it gets ugly:

您可以重写正则表达式以仅使用一个捕获组,尽管它变得丑陋:

@Given("^(I(?='m| am)|.+(?= is))(?:'m| am| is) a user$")

回答by CTester

Arity mismatch:does nothing to do with your regex, rather your Given I'm a userline is mismatching with your method signature public void aUserExists(String username) throws Throwable { }take out the string argument and try again.

Arity mismatch:与您的正则表达式无关,而是您的Given I'm a user行与您的方法签名不匹配,public void aUserExists(String username) throws Throwable { }取出字符串参数并重试。