我有一段代码将在多个集成测试中重复使用。代码将在测试之前和之后运行。集成测试中的Autowire junit规则
我决定使用JUnit @Rule将是实现此目的的最佳方法。
问题是规则将需要访问几个@Autowired spring bean。 (测试与Spring集成测试运行运行,从而自动装配工作正常
我有一个规则:
public class CustomSpringRule extends ExternalResource {
private final SomeOtherBean;
public CustomSpringRule(SomeOtherBean someOtherBean) {
this.someOtherBean = someOtherBean;
}
@Override
public void before() {
someOtherBean.someMethod();
}
(...)
}
我有我的背景下,我增加了我的豆:
@Bean
public CustomSpringRule getCustomSpringRule(SomeOtherBean someOtherBean) {
return new CustomSpringRule(someOtherBean)
}
最后,我刚才自动装配测试文件中的规则豆:
@Autowire
@Rule
public CustomSpringRule customSpringRule
这一切工作正常,但我从来没有真正我们ed @ Rule注释,我略微担心JUnit反射和Spring Autowire不能很好地融合在一起,否则会出现一些初看起来不明显的问题。
有没有人有任何建议,这是否有效和安全?
它似乎有点代码味道有测试,需要按特定顺序运行。测试应该是彼此独立的 –