【问题标题】:Spring startup listener which works in junit在junit中工作的Spring启动监听器
【发布时间】:2012-08-07 07:41:06
【问题描述】:
您好,我有一个不支持 spring 和 hibernate 的 Web 应用程序。
我正在寻找一种可能性来调用一些在运行测试时执行的启动函数。
我使用了 AbstractTransactionalJUnit4SpringContextTests 类并尝试了以下接口
- 应用监听器
- 生命周期
- ServletContextListener
但是没有一个在junit下被调用。
任何提示(hibernate-database-acces 此时应该可用)?
【问题讨论】:
标签:
spring
unit-testing
application-lifecycle
【解决方案1】:
您可以使用 Annotation ContextConfiguration 为您的测试加载应用程序上下文。
@ContextConfiguration(locations="test/test-context.xml")
public class MyTestsClass {
// class body...
}
问候
迈克尔
【解决方案2】:
我的解决方案:ApplicationContextAware,只需在你的类路径中添加一个类:
public class SpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext CONTEXT;
public void setApplicationContext(ApplicationContext context) throws BeansException {
CONTEXT = context;
// DO DB INIT STUFF
}