### Question:
LoginFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; UserManager userManager = (UserManager) httpServletRequest .getSession().getAttribute("userManager"); if (userManager != null) { if (userManager.isLoggedIn()) { LOGGER.debug("user is logged in"); filterChain.doFilter(servletRequest, servletResponse); } else { LOGGER.debug("user is not logged in"); httpServletResponse.sendRedirect(httpServletRequest .getContextPath() + LOGIN_PAGE); } } else { LOGGER.debug("userManager not found"); httpServletResponse.sendRedirect(httpServletRequest .getContextPath() + LOGIN_PAGE); } }  @Override void doFilter(ServletRequest servletRequest,
            ServletResponse servletResponse, FilterChain filterChain); @Override void init(FilterConfig arg0); @Override void destroy(); static final String LOGIN_PAGE; }### Answer:
@Test public void testDoFilterUserManagerNull() throws IOException, ServletException { userManager = null; when(httpSession.getAttribute("userManager")).thenReturn(userManager); loginFilter.doFilter(httpServletRequest, httpServletResponse, filterChain); verify(httpServletResponse).sendRedirect( CONTEXT_PATH + LoginFilter.LOGIN_PAGE); }
@Test public void testDoFilterLoggedIn() throws IOException, ServletException { userManager = new UserManager(); userManager.setUserId("admin"); userManager.setUserPassword("1234"); userManager.login(); when(httpSession.getAttribute("userManager")).thenReturn(userManager); loginFilter.doFilter(httpServletRequest, httpServletResponse, filterChain); verify(filterChain).doFilter(httpServletRequest, httpServletResponse); }
@Test public void testDoFilterNotLoggedIn() throws IOException, ServletException { userManager = new UserManager(); when(httpSession.getAttribute("userManager")).thenReturn(userManager); loginFilter.doFilter(httpServletRequest, httpServletResponse, filterChain); verify(httpServletResponse).sendRedirect( CONTEXT_PATH + LoginFilter.LOGIN_PAGE); }