java - 如何在最新的 4.x 版本中将自定义数据类型传递给 cucumber-jvm stepdef
问题描述
我最近在我的项目中升级到最新的 4.x 版本的 cucumber-jvm 以利用 cucumber 的并行执行特性。但是关于将自定义数据类型作为参数,我现在面临这个问题。早些时候我们有一个接口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);
}
随机字符串类:
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
.
推荐阅读
- c - 如何在为外围设备 (UART) 解耦应用程序和驱动程序代码的同时构建嵌入式应用程序?
- c - 如何为C中的结构数组赋值?
- python - 通过使用 Flask 触发表单操作来抑制重定向
- java - Eclipse 中可运行 jar 文件的问题:无法构造应用程序实例
- android - 无法将项目与 Gradle 文件同步(重新下载依赖项和同步项目 Gradle 问题)
- flutter - 过滤 Firestore 流结果
- java - BroadcastReceivers 究竟可以在哪里中断代码?
- directus - Directus多对多显示模板未显示
- docker - 构建 docker 容器返回错误
- r - 有没有办法选择性地应用这个 stringr 函数?