I'm writing in java and using cucumber with eclipse to search for an IP-like string with the following requirements
should accept four-digit sequences separated by periods where a digit sequence is defined as follows: Any single digit, Any two-digit characters if the first character is non-zero, A one followed by a zero, one, or two followed by any digit
by writing the appropriate regular expression in the Stepdefs.java file, this is what I wrote
@When("^test_ip_address ((?:(\\d)|(1[0-2]\\d)|([1-9]\\d))\\.){3}(?:(\\d)|([1-9]\\d)|(1[0-2]\\d))$")
public void test_ip_address(String arg1) throws Throwable {
System.out.println("test_ip_address true for: " + arg1);
}
now when I write tests (in Gherkin language) for this method in the Test.feature file the first test always fail, the tests (which should all pass )
When test_ip_address 1.2.3.4
When test_ip_address 123.34.76.109
When test_ip_address 123.34.76.109
When test_ip_address 105.22.33.44
it's not a matter of value like when I re-order those tests it's always the first one only that fails even if I used the exact same value in another test it passes ! this is the error I get
cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'skeleton.Stepdefs.test_ip_address(String) in file:(file path..)' with pattern [^test_ip_address ((?:(\d)|(1[0-2]\d)|([1-9]\d))\.){3}(?:(\d)|([1-9]\d)|(1[0-2]\d))$] is declared with 1 parameters. However, the gherkin step has 7 arguments [3., 3, null, null, 4, null, null]
i have searched about the errors and it's thrown when the number of arguments in the test is not the same as in the method,even when i'm using (?:) to pass the string as one argument, I don't know where those 7 arguments came from ! nor the cause of error
cucumber.runtime.CucumberException: Arity mismatch: Step Definition 'skeleton.Stepdefs.test_ip_address(String) in file:(file path..)' with pattern [^test_ip_address ((?:(\d)|(1[0-2]\d)|([1-9]\d))\.){3}(?:(\d)|([1-9]\d)|(1[0-2]\d))$] is declared with 1 parameters. However, the gherkin step has 7 arguments [3., 3, null, null, 4, null, null]
This error is due to the regular expression is having 7 groups. which capture 7 parameters. And the method
public void test_ip_address(String arg1) throws Throwable
only declares 1 parameter (arg1
).
To capture only one parameter, use non capturing group to avoid capturing unnecessary group as method argument.
The expression should look like this:
@When("^test_ip_address ((?:(?:(?:\\d)|(?:1[0-2]\\d)|(?:[1-9]\\d))\\.){3}(?:(?:\\d)|(?:[1-9]\\d)|(?:1[0-2]\\d)))$")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With