Spring startup listener which works in junit
-
25-06-2021 - |
Question
Hi i have an web application unsing spring and hibernate. I'm looking for a possiblity to call some startup functions which are executed when running tests, too.
I uste the AbstractTransactionalJUnit4SpringContextTests class and tried the following interfaces
- ApplicationListener
- Lifecycle
- ServletContextListener
but none of them is called under junit.
Any tips (hibernate-database-acces should be available at this time)?
Solution 2
My solution: ApplicationContextAware, just add a class to your classpath:
public class SpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext CONTEXT;
public void setApplicationContext(ApplicationContext context) throws BeansException {
CONTEXT = context;
// DO DB INIT STUFF
}
OTHER TIPS
You can use the Annotation ContextConfiguration to load the application Context for your Tests.
@ContextConfiguration(locations="test/test-context.xml")
public class MyTestsClass {
// class body...
}
Regards Michael
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow