### Question:
LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter("j_username"); String pass = request.getParameter("j_password"); if (getAuthenticator().isValidLogin(user, pass)) { request.getSession().setAttribute("username", user); response.sendRedirect("/frontpage"); } else { forwardTo("/invalidlogin", request, response); } }    }### Answer:
@Test public void wrongPasswordShouldRedirectToErrorPage() throws Exception { request.addParameter("j_username", VALID_USERNAME); request.addParameter("j_password", "wrongpassword"); servlet.service(request, response); assertUserEndedUpOnPage("/invalidlogin"); }
@Test public void validLoginForwardsToFrontPageAndStoresUsername() throws Exception { request.addParameter("j_username", VALID_USERNAME); request.addParameter("j_password", CORRECT_PASSWORD); servlet.service(request, response); assertUserEndedUpOnPage("/frontpage"); assertEquals(VALID_USERNAME, request.getSession() .getAttribute("username")); }