AdminConsoleController { private ModelAndView login() { return login("Please log in"); } @Autowired  AdminConsoleController(XMPPTCPConnectionConfiguration connectionConfiguration); @RequestMapping("/") ModelAndView index(HttpSession session); @RequestMapping(value = "/{command}", method = RequestMethod.GET) ModelAndView command(@PathVariable("command") String command, HttpSession session); @RequestMapping(value = "/{command}", method = RequestMethod.POST) ModelAndView submitCommand(@PathVariable("command") String command, HttpServletRequest request,
            HttpSession session); @RequestMapping(value = "/login", method = RequestMethod.POST) ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password,
            HttpSession session); @RequestMapping(value = "/logout") ModelAndView logout(HttpSession session); static final String SESSION_ATTRIBUTE; static final String SESSION_FIELD; static final Map<String, String> COMMANDS; }
@Test public void login() throws XMPPException, InterruptedException, IOException, SmackException { ModelAndView mav = controller.login(ENTITY, PASSWORD, session); Mockito.verify(connection).connect(); Mockito.verify(connection).login(ENTITY, PASSWORD); Assert.assertNotNull(session.getAttribute(AdminConsoleController.SESSION_ATTRIBUTE)); Assert.assertEquals("redirect:", mav.getViewName()); }
@Test public void failedLogin() throws XMPPException, InterruptedException, IOException, SmackException { XMPPException xmppException = mock(XMPPException.class); Mockito.doThrow(xmppException).when(connection).login(ENTITY, PASSWORD); ModelAndView mav = controller.login(ENTITY, PASSWORD, session); Mockito.verify(connection).connect(); Mockito.verify(connection).login(ENTITY, PASSWORD); Assert.assertNull(session.getAttribute(AdminConsoleController.SESSION_ATTRIBUTE)); Assert.assertEquals("index", mav.getViewName()); Assert.assertNotNull(mav.getModel().get("error")); Assert.assertNull(mav.getModel().get("authenticated")); }
@Test public void failedConnect() throws XMPPException, InterruptedException, IOException, SmackException { XMPPException xmppException = mock(XMPPException.class); Mockito.doThrow(xmppException).when(connection).connect(); ModelAndView mav = controller.login(ENTITY, PASSWORD, session); Mockito.verify(connection).connect(); Assert.assertNull(session.getAttribute(AdminConsoleController.SESSION_ATTRIBUTE)); Assert.assertEquals("index", mav.getViewName()); Assert.assertNotNull(mav.getModel().get("error")); Assert.assertNull(mav.getModel().get("authenticated")); }