Java-如何在最新的4.x版本中将自定义数据类型传递给cucumber-jvm stepdef
作者:互联网
我最近在我的项目中升级到了黄瓜jvm的最新4.x版本,以便利用黄瓜的并行执行功能.但是,关于将自定义数据类型作为参数,我现在面临这个问题.之前我们有一个名为Transformer的接口,可以为自定义数据类型实现该接口,现在在最新版本中,我发现了需要实现的TypeRegistryConfigurer接口.但这并没有像我期望的那样认识到这一步.详细信息如下:
小黄瓜步骤:
Given user gets random(3,true,true) parameter
步骤定义:
@Given("user gets {random} parameter")
public void paramTest(RandomString randomString) {
System.out.println(randomString.string);
}
RandomString类:
public class RandomString {
public String string;
public RandomString(String string) {
Matcher m = Pattern.compile("random\\((.?)\\)").matcher(string);
String t = "";
while (m.find()) {
t = m.group(1);
}
boolean isAlpha = true, isNum = true;
if (t.length() > 0) {
String[] placeholders = t.split(",");
if (placeholders.length == 3) {
int count = Integer.parseInt(placeholders[0]);
isAlpha = Boolean.valueOf(placeholders[1]);
isNum = Boolean.valueOf(placeholders[2]);
this.string = string.replaceAll("random(.*)", RandomStringUtils.random(count, isAlpha, isNum));
}
}
this.string = string.replaceAll("random(.*)", RandomStringUtils.random(3, isAlpha, isNum));
}
}
TypeRegistryImpl:
public class TypeRegistryConfiguration implements TypeRegistryConfigurer {
@Override
public Locale locale() {
return Locale.ENGLISH;
}
@Override
public void configureTypeRegistry(TypeRegistry typeRegistry) {
typeRegistry.defineParameterType(new ParameterType<>(
"random",
"random([0-9],true|false,true|false)",
RandomString.class,
RandomString::new)
);
}
}
解决方法:
您的字符串random(3,true,true)与以下模式中使用的模式不匹配:
typeRegistry.defineParameterType(new ParameterType<>(
"random",
"random([0-9],true|false,true|false)",
RandomString.class,
RandomString::new)
);
您可以通过创建模式并对其进行测试来验证这一点:
import java.util.regex.Pattern;
class Scratch {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("random([0-9],true|false,true|false)");
// prints out false
System.out.println(pattern.matcher("random(3,true,true)").matches());
}
}
您还没有在RandomString中使用匹配模式.
标签:cucumber-jvm,cucumber,java 来源: https://codeday.me/bug/20191024/1923042.html