ShutdownListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent event) { SessionManager sessionManager = (SessionManager)event.getServletContext().getAttribute(Attributes.SESSION_MANAGER); if (sessionManager != null) { sessionManager.close(); } }  @Override void contextDestroyed(ServletContextEvent event); @Override void contextInitialized(ServletContextEvent event);  }
@Test public void testContextDestroyed() { ServletContextEvent event = new ServletContextEvent(context); listener.contextDestroyed(event); verify(manager, never()).close(); }
@Test public void testContextDestroyedWithSessionManager() { when(context.getAttribute(Attributes.SESSION_MANAGER)).thenReturn(manager); ServletContextEvent event = new ServletContextEvent(context); listener.contextDestroyed(event); verify(manager).close(); }