我试图将在spring上下文中配置的规则注入到spock规范中。
@ContextConfiguration(locations=["spring.xml"])
class TestSpec extends Specification {
@Rule @Inject //@Autowired
public ActivitiRule activitiRule;
@Deployment
def "Process test"() {
when:
//...
then:
//...
}
}问题是spock自己实例化规则,而没有遵守@Inject或@Autowired注释。在JUnit中,这与预期的一样。有可能将规则注入spock吗?
发布于 2013-07-10 06:46:11
有趣的用例。问题是Spock的Rule扩展在Spock的Spring扩展注入规则之前就调用了它。也许可以解决这个问题。如果你不介意,请在http://issues.spockframework.org上提交一个问题。目前,只有注入MethodRules (而不是TestRules)才能按预期工作。
发布于 2013-07-11 01:48:43
只要这不是开箱即用的(参见Issue),我发现的一种可能的解决方法是使用Spring TestExecutionListener而不是TestRule,这当然只有在您自己编写规则时才有用。
在TestExecutionListener中,您可以访问ApplicationContext,并可以获得所需的任何bean。
https://stackoverflow.com/questions/17545264
复制相似问题