### Question:
StartupServletContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent event) { if (_facesInitializer != null && _servletContext != null) { FacesContext facesContext = _facesInitializer.initShutdownFacesContext(_servletContext); dispatchInitializationEvent(event, FACES_INIT_PHASE_PREDESTROY); _facesInitializer.destroyFaces(_servletContext); if (facesContext != null) { _facesInitializer.destroyShutdownFacesContext(facesContext); } FactoryFinder.releaseFactories(); dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTDESTROY); } _servletContext = null; }  @Override void contextInitialized(ServletContextEvent event); @Override void contextDestroyed(ServletContextEvent event); void setFacesInitializer(FacesInitializer facesInitializer);  }### Answer:
@Test public void testContextDestroyedInitializerCalled() { _setServletContext(servletContext); FacesInitializer initializer = EasyMock.createMock(FacesInitializer.class); EasyMock.expect(initializer.initShutdownFacesContext(servletContext)).andReturn(facesContext).once(); initializer.destroyFaces(servletContext); EasyMock.expectLastCall().once(); initializer.destroyShutdownFacesContext(facesContext); EasyMock.expectLastCall().once(); EasyMock.replay(initializer); _setFacesInitializer(initializer); _listener.contextDestroyed(new ServletContextEvent(servletContext)); EasyMock.verify(initializer); }
@Test public void testContextDestroyedFacesContextAvailable() { _setServletContext(servletContext); facesContext.release(); final FacesInitializer realInitializer = FacesInitializerFactory.getFacesInitializer(servletContext); final AssertFacesContextAnswer assertAnswer = new AssertFacesContextAnswer(); FacesInitializer mockInitializer = EasyMock.createMock(FacesInitializer.class); EasyMock.expect(mockInitializer.initShutdownFacesContext(servletContext)) .andAnswer(new IAnswer<FacesContext>() { public FacesContext answer() throws Throwable { assertAnswer.facesContext = realInitializer.initShutdownFacesContext(servletContext); return assertAnswer.facesContext; } }); mockInitializer.destroyFaces(servletContext); EasyMock.expectLastCall().andStubAnswer(assertAnswer); mockInitializer.destroyShutdownFacesContext(EasyMock.isA(FacesContext.class)); EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>() { public Object answer() throws Throwable { FacesContext argCtx = (FacesContext) EasyMock.getCurrentArguments()[0]; Assert.assertNotNull(argCtx); Assert.assertEquals(assertAnswer.facesContext, argCtx); realInitializer.destroyShutdownFacesContext(argCtx); return null; } }); EasyMock.replay(mockInitializer); _setFacesInitializer(mockInitializer); _listener.contextDestroyed(new ServletContextEvent(servletContext)); EasyMock.verify(mockInitializer); Assert.assertNull(FacesContext.getCurrentInstance()); }