LoginController { @RequestMapping(method = RequestMethod.POST) public String handleLogin(@RequestParam String username, @RequestParam String password, HttpSession session) throws AuthenticationException { Account account = this.accountService.login(username, password); session.setAttribute(ACCOUNT_ATTRIBUTE, account); String url = (String) session.getAttribute(REQUESTED_URL); session.removeAttribute(REQUESTED_URL); if (StringUtils.hasText(url) && !url.contains("login")) { return "redirect:" + url; } else { return "redirect:/index.htm"; } }  @RequestMapping(method = RequestMethod.GET) void login(); @RequestMapping(method = RequestMethod.POST) String handleLogin(@RequestParam String username, @RequestParam String password, HttpSession session); static final String ACCOUNT_ATTRIBUTE; static final String REQUESTED_URL; }
@Test public void testHandleLogin() throws AuthenticationException { MockHttpSession mockHttpSession = new MockHttpSession(); mockHttpSession.setAttribute(LoginController.REQUESTED_URL, "someUrl"); String view = this.loginController.handleLogin("john", "secret", mockHttpSession); Account account = (Account) mockHttpSession.getAttribute(LoginController.ACCOUNT_ATTRIBUTE); assertNotNull(account); assertEquals("John", account.getFirstName()); assertEquals("Doe", account.getLastName()); assertNull(mockHttpSession.getAttribute(LoginController.REQUESTED_URL)); assertEquals("redirect:someUrl", view); mockHttpSession = new MockHttpSession(); view = this.loginController.handleLogin("john", "secret", mockHttpSession); assertEquals("redirect:/index.htm", view); mockHttpSession = new MockHttpSession(); mockHttpSession.setAttribute(LoginController.REQUESTED_URL, "abclogindef"); view = this.loginController.handleLogin("john", "secret", mockHttpSession); assertEquals("redirect:/index.htm", view); }