### Question:
PushContinuationFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { PushContinuation pushContinuation = factory.createContinuation(request); loggerService.startSession(); loggerService.defineRequestId(pushContinuation.getReqId()); request.setAttribute(RequestProperties.CONTINUATION, pushContinuation); chain.doFilter(request, response); } finally { loggerService.closeSession(); } } @Inject @VisibleForTesting  PushContinuationFilter(LoggerService loggerService, PushContinuation.Factory factory); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy();  }### Answer:
@Test public void doFilterShouldCloseSession() throws Exception { int requestId = 5; expect(continuation.getReqId()).andReturn(requestId); request.setAttribute(RequestProperties.CONTINUATION, continuation); expectLastCall(); expect(continuationFactory.createContinuation(request)).andReturn(continuation); loggerService.startSession(); expectLastCall(); loggerService.defineRequestId(requestId); expectLastCall(); chain.doFilter(request, response); expectLastCall(); loggerService.closeSession(); expectLastCall(); control.replay(); testee.doFilter(request, response, chain); control.verify(); }
@Test(expected=IllegalStateException.class) public void doFilterShouldCloseSessionWhenExceptionByChain() throws Exception { int requestId = 5; expect(continuation.getReqId()).andReturn(requestId); request.setAttribute(RequestProperties.CONTINUATION, continuation); expectLastCall(); expect(continuationFactory.createContinuation(request)).andReturn(continuation); loggerService.startSession(); expectLastCall(); loggerService.defineRequestId(requestId); expectLastCall(); chain.doFilter(request, response); expectLastCall().andThrow(new IllegalStateException("Might be exception")); loggerService.closeSession(); expectLastCall(); control.replay(); try { testee.doFilter(request, response, chain); } catch (Exception e) { control.verify(); throw e; } }
@Test(expected=IllegalStateException.class) public void doFilterShouldCloseSessionWhenExceptionByFactory() throws Exception { expect(continuationFactory.createContinuation(request)).andThrow(new IllegalStateException()); loggerService.closeSession(); expectLastCall(); control.replay(); try { testee.doFilter(request, response, chain); } catch (Exception e) { control.verify(); throw e; } }