我正在使用 JUnit 进行一些集成测试。我有一个 xml 文件,在其中声明了一些我想要注入(inject)的模板实体。在每个测试中,我都需要一个新的应用程序上下文,即任何测试都不应该依赖于/与其他测试耦合,因此在 setUp()
上,我需要在这些实体的默认起始值上“重新初始化” .
我可以通过重新加载来实现,但是这样我就不能使用@Autowired
注解了。
@Before
public void setUp(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:entityTemplates-Context.xml");
homeA = (Home)ctx.getBean("homeA");
}
有没有办法做到这一点,同时仍然对这些实体使用@Autowired
?
最佳答案
您了解过 Spring 对集成测试的支持吗?
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html
我的团队将其用于我们需要 Spring 容器的集成测试。每次测试您都会获得新的上下文,但这会显着减慢测试速度。
关于java - 刷新 Spring ApplicationContext,而不重新初始化上下文,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9734948/