Search code examples
springunit-testingapplication-lifecycle

Spring startup listener which works in junit


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

  • 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
        }