### Question:
StartupServletContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent event) { if (_servletContext != null) { throw new IllegalStateException("context is already initialized"); } _servletContext = event.getServletContext(); Boolean b = (Boolean) _servletContext.getAttribute(FACES_INIT_DONE); if (b == null || b == false) { long start = System.currentTimeMillis(); if (_facesInitializer == null) { _facesInitializer = FacesInitializerFactory.getFacesInitializer(_servletContext); } FacesContext facesContext = _facesInitializer.initStartupFacesContext(_servletContext); dispatchInitializationEvent(event, FACES_INIT_PHASE_PREINIT); _facesInitializer.initFaces(_servletContext); dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTINIT); _servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE); _facesInitializer.destroyStartupFacesContext(facesContext); log.log(Level.INFO, "MyFaces Core has started, it took [" + (System.currentTimeMillis() - start) + "] ms."); } else { log.finest("MyFaces already initialized"); } }  @Override void contextInitialized(ServletContextEvent event); @Override void contextDestroyed(ServletContextEvent event); void setFacesInitializer(FacesInitializer facesInitializer);  }### Answer:
@Test public void testContextInitializedInitializerCalled() { FacesInitializer initializer = EasyMock.createMock(FacesInitializer.class); EasyMock.expect(initializer.initStartupFacesContext(servletContext)).andReturn(facesContext).once(); initializer.initFaces(servletContext); EasyMock.expectLastCall().once(); initializer.destroyStartupFacesContext(facesContext); EasyMock.expectLastCall().once(); EasyMock.replay(initializer); _setFacesInitializer(initializer); _listener.contextInitialized(new ServletContextEvent(servletContext)); EasyMock.verify(initializer); }
@Test public void testContextInitializedFacesContextAvailable() { facesContext.release(); final FacesInitializer realInitializer = FacesInitializerFactory.getFacesInitializer(servletContext); final AssertFacesContextAnswer assertAnswer = new AssertFacesContextAnswer(); FacesInitializer mockInitializer = EasyMock.createMock(FacesInitializer.class); EasyMock.expect(mockInitializer.initStartupFacesContext(servletContext)) .andAnswer(new IAnswer<FacesContext>() { public FacesContext answer() throws Throwable { assertAnswer.facesContext = realInitializer.initStartupFacesContext(servletContext); return assertAnswer.facesContext; } }); mockInitializer.initFaces(servletContext); EasyMock.expectLastCall().andStubAnswer(assertAnswer); mockInitializer.destroyStartupFacesContext(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.destroyStartupFacesContext(argCtx); return null; } }); EasyMock.replay(mockInitializer); _setFacesInitializer(mockInitializer); _listener.contextInitialized(new ServletContextEvent(servletContext)); EasyMock.verify(mockInitializer); Assert.assertNull(FacesContext.getCurrentInstance()); }