Java Cucumber-与类型注册表配置文件共享上下文

Java Cucumber-与类型注册表配置文件共享上下文,java,spring,dependency-injection,cucumber,Java,Spring,Dependency Injection,Cucumber,我想要的:我想要在传统上命名为“TypeRegistryConfiguration”的文件中使用spring@Autowired注释。它对steps文件非常有效,但由于某些原因,依赖项注入在该文件中不起作用(即使在调试级别也没有错误/警告消息)。Spring扫描“com.funky.steps”,其中包含步骤、上下文和类型注册表配置文件,请参见下面的示例 背景: package com.funky.steps.context; @组成部分 公共类公共上下文{ ... 类型注册表配置: pack

我想要的:我想要在传统上命名为“TypeRegistryConfiguration”的文件中使用spring@Autowired注释。它对steps文件非常有效,但由于某些原因,依赖项注入在该文件中不起作用(即使在调试级别也没有错误/警告消息)。Spring扫描“com.funky.steps”,其中包含步骤、上下文和类型注册表配置文件,请参见下面的示例

背景:

package com.funky.steps.context;
@组成部分
公共类公共上下文{
...
类型注册表配置:

package com.funky.steps.typeregistry;
公共类TypeRegistryConfiguration实现TypeRegistryConfigurer{
@自动连线
私有CommonContext上下文;//未注入!
@凌驾
公共区域设置(){
返回Locale.ENGLISH;
}
@凌驾
公共无效配置类型注册表(类型注册表类型注册表){
registerStuff(类型注册表)
}
...
步骤:

package com.funky.steps;
公共类WebServiceSteps{
@自动连线
私有CommonContext上下文;//正确注入
...

为什么需要它:我有一些步骤可以将变量保存在上下文中供以后使用。当我使用类型注册表构建对象时,我希望能够访问这些变量。例如:

Given I call the web service 1
And the response field "id" will be used as "$id" # id is saved in the context
When I call the web service 2: # call type registry configuration to build the request using $id (which I can not access because it is in the context and @Autowired is not working)
| id | $id |
Then ...

我想要的现在是不可能的。看:


这在Cucumber 4.x中是不可能的,但您可以在Cucumber 5.0.0-RC1中注册参数和数据表类型作为粘合的一部分

您可以使用
@ParameterType
,而不是在注册表中注册参数类型。数据表类型也是如此

private final Catalog catalog; 
private final Basket basket;

@ParameterType("[a-z ]+")
public Catalog catalog(String name) {
  return catalogs.findCatalogByName(name);
}

@ParameterType("[a-z ]+")
public Product product(String name) {
  return catalog.findProductByName(name);
}

@Given("the {catalog} catalog")
public void the_catalog(Catalog catalog){
  this.catalog = catalog
}

@When("a user places the {product} in his basket")
public void a_user_place_the_product_in_his_basket(Product product){
  basket.add(product);
}
注意:方法名用作参数名。还可以通过@ParameterType的name属性提供参数名


请参阅:

您所寻找的任何东西都可以通过cucumber 5、使用qaf cucumber和cucumber 5通过使用qaf cucumber来实现。 下面是一个例子:

Given I call the web service 1
And the response field "id" will be used as "id"
When I call the web service 2:
| id | ${id} |
Then ...
步骤实现可能类似于方法实现:

@And("the response field {sting} will be used as {string}")
public void theResponseFiFeldWillBeUsedAs(String field, String key) {
    //do the need full
    String value = read(field);
    getBundle().setProperty(key, value);
}

@@QAFTestStep("I call the web service 2:")
public void iCallWS2:(Map<String, Object> map) {
    //map should be with key id and resolved value
}
@和(“响应字段{sting}将用作{string}”)
public void响应字段将用作(字符串字段、字符串键){
//你的需求是否充分
字符串值=读取(字段);
getBundle().setProperty(键,值);
}
@@QAFTestStep(“我称之为web服务2:”)
公共空间iCallWS2:(地图){
//映射应具有键id和解析值
}
此外,您还可以从库中获益,它提供了带有参数的概念,使web服务测试变得容易和可维护