### Question:
AuthenticationFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; try { if ("POST".equals(httpRequest.getMethod())) { Credentials credentials = authentication(httpRequest); loggerService.defineUser(credentials.getUser()); httpRequest.setAttribute(RequestProperties.CREDENTIALS, credentials); } chain.doFilter(request, response); } catch (AuthenticationException e) { logger.info(e.getMessage()); httpErrorResponder.returnHttpUnauthorized(httpRequest, httpResponse); } catch (Exception e) { logger.info(e.getMessage()); httpErrorResponder.returnHttpServerError(httpRequest, httpResponse); } } @Inject @VisibleForTesting  AuthenticationFilter(AuthenticationService authenticationService, 
			LoggerService loggerService, 
			HttpErrorResponder httpErrorResponder); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain); @Override void destroy();  }### Answer:
@Test public void postCallDoFilterChain() throws Exception { HttpServletRequest request = control.createMock(HttpServletRequest.class); HttpServletResponse response = control.createMock(HttpServletResponse.class); FilterChain chain = control.createMock(FilterChain.class); expectAuthentication(request); expect(request.getMethod()).andReturn("POST"); chain.doFilter(request, response); expectLastCall(); control.replay(); testee.doFilter(request, response, chain); control.verify(); }
@Test public void postCallReturn500WhenRuntimeExceptionByAuth() throws Exception { HttpServletRequest request = control.createMock(HttpServletRequest.class); HttpServletResponse response = control.createMock(HttpServletResponse.class); FilterChain chain = control.createMock(FilterChain.class); expectExceptionByAuth(request, new RuntimeException()); errorResponder.returnHttpServerError(request, response); expectLastCall(); expect(request.getMethod()).andReturn("POST"); control.replay(); testee.doFilter(request, response, chain); control.verify(); }
@Test public void postCallReturn401WhenAuthenticationExceptionByAuth() throws Exception { HttpServletRequest request = control.createMock(HttpServletRequest.class); HttpServletResponse response = control.createMock(HttpServletResponse.class); FilterChain chain = control.createMock(FilterChain.class); expectExceptionByAuth(request, new AuthenticationException()); errorResponder.returnHttpUnauthorized(request, response); expectLastCall(); expect(request.getMethod()).andReturn("POST"); control.replay(); testee.doFilter(request, response, chain); control.verify(); }