### Question:
MyFacesServlet implements Servlet, DelegatedFacesServlet { @Override public void init(ServletConfig servletConfig) throws ServletException { ServletContext servletContext = servletConfig.getServletContext(); if (_facesInitializer == null) { _facesInitializer = FacesInitializerFactory.getFacesInitializer(servletContext); } FacesContext facesContext = _facesInitializer.initStartupFacesContext(servletContext); Boolean b = (Boolean)servletContext.getAttribute(StartupServletContextListener.FACES_INIT_DONE); if (b == null || b == false) { if(log.isLoggable(Level.WARNING)) { log.warning("ServletContextListener not yet called"); } _facesInitializer.initFaces(servletConfig.getServletContext()); } _facesInitializer.destroyStartupFacesContext(facesContext); delegate.init(servletConfig); log.info("MyFacesServlet for context '" + servletConfig.getServletContext().getRealPath("/") + "' initialized."); }  void setFacesInitializer(FacesInitializer facesInitializer); @Override void destroy(); @Override ServletConfig getServletConfig(); @Override String getServletInfo(); @Override void init(ServletConfig servletConfig); @Override void service(ServletRequest request, ServletResponse response);  }### Answer:
@Test public void testInitInitializerCalled() throws ServletException { 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(); ServletConfig servletConfig = EasyMock.createMock(ServletConfig.class); EasyMock.expect(servletConfig.getServletContext()).andReturn(servletContext).anyTimes(); EasyMock.expect(servletConfig.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes(); EasyMock.replay(initializer, servletConfig); _setFacesInitializer(initializer); _servlet.init(servletConfig); EasyMock.verify(initializer); }
@Test public void testInitFacesContextAvailable() throws ServletException { 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; } }); ServletConfig servletConfig = EasyMock.createMock(ServletConfig.class); EasyMock.expect(servletConfig.getServletContext()).andReturn(servletContext).anyTimes(); EasyMock.expect(servletConfig.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes(); EasyMock.replay(mockInitializer, servletConfig); _setFacesInitializer(mockInitializer); _servlet.init(servletConfig); EasyMock.verify(mockInitializer); Assert.assertNull(FacesContext.getCurrentInstance()); }