### Question:
JSSwitchUserProcessingFilter extends SwitchUserFilter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) req; HttpServletResponse httpResponse = (HttpServletResponse) res; if (requiresSwitchUser(httpRequest)) { createAuditEvent(SWITCH_USER.toString()); Authentication targetUser = attemptSwitchUser(httpRequest); addParamsToSwitchUserAuditEvent(targetUser); SecurityContextHolder.getContext().setAuthentication(targetUser); httpResponse.sendRedirect(httpResponse.encodeRedirectURL(httpRequest.getContextPath() + jsTargetUrl)); closeAuditEvent(SWITCH_USER.toString()); themeResolver.setThemeName(httpRequest, httpResponse, null); return; } else if (requiresExitUser(httpRequest)) { createAuditEvent(EXIT_SWITCHED_USER.toString()); Authentication originalUser = attemptExitUser(httpRequest); addParamsToExitUserAuditEvent(originalUser); SecurityContextHolder.getContext().setAuthentication(originalUser); httpResponse.sendRedirect(httpResponse.encodeRedirectURL(httpRequest.getContextPath() + exitTargetUrl)); closeAuditEvent(EXIT_SWITCHED_USER.toString()); themeResolver.setThemeName(httpRequest, httpResponse, null); return; } chain.doFilter(httpRequest, httpResponse); }  void setAuditContext(AuditContext auditContext); void afterPropertiesSet(); void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); void setTargetUrl(String targetUrl); void setExitTargetUrl(String exitTargetUrl); ThemeResolver getThemeResolver(); void setThemeResolver(ThemeResolver themeResolver);  }### Answer:
@Test public void doFilter_userExitOrSwitchIsNotRequired_nextFilterIsInvokedWithRequest() throws IOException, ServletException { objectUnderTest.doFilter(servletRequest, servletResponse, filterChain); verify(filterChain).doFilter(servletRequest, servletResponse); }
@Test public void doFilter_sendRedirectThrowsIOException_IOException() throws IOException { doReturn(CONTEXT_PATH + SWITCH_URL).when(servletRequest).getRequestURI(); doThrow(new IOException()).when(servletResponse).sendRedirect(SWITCH_URL); assertThrows(IOException.class, new Executable() { @Override public void execute() throws IOException, ServletException { objectUnderTest.doFilter(servletRequest, servletResponse, filterChain); } }); }
@Test public void doFilter_chainDoFilterThrowsServletException_servletException() throws IOException, ServletException { doThrow(new ServletException()).when(filterChain).doFilter(servletRequest, servletResponse); assertThrows(ServletException.class, new Executable() { @Override public void execute() throws IOException, ServletException { objectUnderTest.doFilter(servletRequest, servletResponse, filterChain); } }); }