5

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

A.Mahmoud
  • 65
  • 7
  • Does [**this**](https://stackoverflow.com/questions/37041622/what-is-the-error-of-cucumber-runtime-cucumberexception-arity-mismatch-step-de/37042083) answer help? –  Jun 07 '20 at 06:14
  • it is a similar error message , but it's a different issue , he's not capturing arguments but my method is, in fact like i said it works fine beyond the first test – A.Mahmoud Jun 07 '20 at 06:51
  • I tried this `@When("^test_ip_address ((?:(?:(?:\\d)|(?:1[0-2]\\d)|(?:[1-9]\\d))\\.){3}(?:(?:\\d)|([1-9]\\d)|(?:1[0-2]\\d)))$")` is working – samabcde Jun 07 '20 at 08:13
  • Try `^test_ip_address (?:(?:\\d|1[0-2]\\d|[1-9]\\d)\\.){3}(?:\\d|[1-9]\\d|1[0-2]\\d)$` SEe https://regex101.com/r/SPljQv/1 – The fourth bird Jun 07 '20 at 11:30
  • @samabcde still getting the same error, only difference is that it's now "declared with 1 parameters. However, the gherkin step has 2 arguments [123.34.76.109, null]. " which looks more promising, also it would be of much help if you told me what would be a possible reason for a test to always fail the first time and then works properly, **Edit** when i changed the input the arguments captured were : [105.22.33.44, 44] – A.Mahmoud Jun 07 '20 at 14:31
  • @Thefourthbird also same error different number of arguments 'the gherkin step has 0 arguments [] ' i don't think changing the regex would change the result ,what i'm trying to understand is why the first gherkin statement parsed in a wrong way , then the remaining are parsed normally – A.Mahmoud Jun 07 '20 at 14:45
  • 1
    What is your version for cucumber? Can you explain (may be show output of test) on "fail the first time and then works properly".And I find I forget to add one ?: in the expression. Please try again `@When("^test_ip_address ((?:(?:(?:\\d)|(?:1[0-2]\\d)|(?:[1-9]\\d))\\.){3}(?:(?:\\d)|(?:[1-9]\\d)|(?:1[0-2]\\d)))$")`. What I do is to make sure only one group is captured, hence to solve the "the gherkin step has x arguments" error. – samabcde Jun 07 '20 at 14:56
  • thank you,it works ! if i'm not asking too much could you explain why my expression was not capturing the pattern (only the first time !) that made me think my expression was just fine because how else would it work on later Gherkin tests ? – A.Mahmoud Jun 07 '20 at 15:18
  • I am testing with cucumber-java 5.6.0, and I have different error "...the gherkin step has 4 arguments:" (not 7) using your expression. Also I don't see any other test works. So I need to use the same version as yours to see why it fails only in first time. – samabcde Jun 07 '20 at 15:34
  • i'm not using the cucumber independent software, i'm integrating it into gradle on eclipse, but it has something to do with (?:) in your answer i see you have distributed it all over the expression and apparently i haven't used it properly """ @When("^match_four ((in|out)(foo|bar))$") the associated Java function will require three parameters: one for the 'in/out' value, one for the 'foo/bar' value, and one for their concatenation. To only capture the concatenated value, use: @When("^match_four ((?:in|out)(?:foo|bar))$") """ – A.Mahmoud Jun 07 '20 at 18:11

3 Answers3

5
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)))$")
samabcde
  • 6,988
  • 2
  • 25
  • 41
0

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 :)

Sahith Kurapati
  • 1,617
  • 10
  • 14
0

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

Justin Lambert
  • 940
  • 1
  • 7
  • 13