views:

258

answers:

1

I have a spring mvc application and we run tests with it using jetty.

Sometimes the loading of the context totally fails, because bad xml or because Class Not Found exception or because a bean throws an exception in the constructor, setter or init method.

In such a case I would like to to stop the application with System.exit(1) or take some other drastic measure and not have the test cycle continue any further.

How can that be done?

+2  A: 

How are you loading the context? Via context listener? Define your own (extend ContextLoaderListener and override contextInitialized()) and invoke System.exit(1) when context initialization fails:

@Override
public void contextInitialized(ServletContextEvent event) {
    try {
      super.contextInitialized(event);
    } catch (Throwable T) {
      T.printStackTrace();
      System.exit(1);
    }
}

Update For DispatcherServlet, use the same approach but override the initWebApplicationContext() method:

@Override
protected WebApplicationContext initWebApplicationContext() throws BeansException {
    try {
      super.initWebApplicationContext();
    } catch (Throwable T) {
      T.printStackTrace();
      System.exit(1);
    }
}
ChssPly76
I am not loading the context by myself. The Dispatcher Servlet does it.
flybywire
@flybywire - that's why I've asked how your context is getting initialized. See my update.
ChssPly76