我有这样的特征声明:
Feature: find taxi and minicabs information
in order to get taxi and minicabs contact at given location
as application developer
I want to find tax and minicabs contact information at given location or query options
Scenario Outline: find taxi and minicabs contact information
Given Joe at location with <lat> and <lon>
When get all taxi and minicabs contacts information
Then should see list of taxi and minicabs
And all of them are at location with <lat> and <lon>
Examples:
| lat | lon |
| 51.490075 | -0.133226 |
我有这样的步骤定义:
@Given("^Joe at location with ([+-]?([0-9]+[.])?[0-9]+) and ([+-]?([0-9]+[.])?[0-9]+)$")
public void joeAtLocationWithLatAndLon(Number lat, Number lon) throws Throwable {
....
}
我预计我可以收到2个参数,但Cucumber传递给我4个参数。 错误信息如下:
with pattern [^Joe at location with ([+-]?([0-9]+[.])?[0-9]+) and ([+-]?([0-9]+[.])?[0-9]+)$] is declared with 2 parameters. However, the gherkin step has 4 arguments [51.490075, 51., -0.133226, 0.].
你对此有任何想法吗?顺便说一句,如果你能解释黄瓜识别参数数量的方式或者分享我的任何文件,我非常感谢。
答案 0 :(得分:2)
问题是正则表达式中的两个内括号。使用当前的正则表达式,你将得到2组 - 一个整体“51.490075”和第二个“51”。它匹配([0-9]+[.])
部分中的exp。因此创建了4个参数。
删除内部括号,每个只有一个参数,总共两个。
你将要遇到的下一个问题是,除非你告诉它,否则黄瓜不知道如何将String转换为Number类。为此,您需要使用Transform注释并为此创建一个特定的类。
import cucumber.api.Transformer;
public class NumberTransformer extends Transformer<Number>{
@Override
public Number transform(String value) {
return Double.parseDouble(value);
}
}
@Given("^Joe at location with ([+-]?[0-9]+[.]?[0-9]+) and ([+-]?[0-9]+[.]?[0-9]+)$")
public void joeAtLocationWithAnd(@Transform(NumberTransformer.class)Number arg1, @Transform(NumberTransformer.class)Number arg2) throws Exception {
System.out.println(arg1);
System.out.println(arg2);
}
对于转换问题,您还可以查找xstreams。如果您使用黄瓜2,使用Xstreamconvertor注释可以更轻松地进行这种转换 - https://github.com/cucumber/cucumber-jvm/pull/1010