regular expression with cucumber doesn't match the string giving error but would match the same string if the test is written more than once

Viewed 1608

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

3 Answers
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)))$")

The following regex expression should validate all IPv4 addresses:

String zeroTo255 = "(\\d{1,2}|(0|1)\\" + "d{2}|2[0-4]\\d|25[0-5])"; 

String regex = zeroTo255 + "\\."  + zeroTo255 + "\\."  + zeroTo255 + "\\." + zeroTo255;

Use this regex variable instead. Hopefully this answered your question :)

We dont need edit regular expressions because that is converted from our feature file as step definition file

Related