将规则注入spock规范

时间:2013-07-09 09:54:14

标签: spring rule spock

我正在尝试将一个在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?

2 个答案:

答案 0 :(得分:1)

有趣的用例。问题是Spock的Rule扩展在Spock的Spring扩展注入它之前调用了规则。有可能解决这个问题。如果您不介意,请在http://issues.spockframework.org提交问题。目前,仅注入MethodRule s(而非TestRule s)将按预期工作。

答案 1 :(得分:1)

只要开箱即用(参见Issue),我找到的一种可能的解决方法是使用Spring TestExecutionListener而不是TestRule,这当然只有在你写作时才有用自己的规则。

在TestExecutionListener中,您可以访问ApplicationContext并获取所需的任何bean。

相关问题