HomeController { @RequestMapping(value = "index.html", method = RequestMethod.GET) public String index(Model model, HttpServletRequest request) { AuthenticatedUser currentUser = userContext.getCurrentUser(); if (!currentUser.getUsername().equals("anonymousUser")){ Users users=usersRepository.loadUserByUsername(currentUser.getUsername()); if(users!=null){ if(users.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) return "redirect:/Administrator/adminHome.html"; else if (users.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_USER"))) return "redirect:/patients/home.html"; else if (users.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_SYSADMIN"))) return "redirect:/sysadmin/valueSetList"; } } if (request.getParameter("expired") != null) { model.addAttribute("expired", true); } if (request.getSession().getAttribute("tokenErrorMessage") != null) { model.addAttribute("tokenErrorMessage", request.getSession().getAttribute("tokenErrorMessage")); } return "views/index"; }  @RequestMapping(value = "/", method = RequestMethod.GET, produces = "text/html") String home(); @RequestMapping(value = "/keep-alive", method = RequestMethod.GET) @ResponseBody void keepAlive(); @RequestMapping(value = "index.html", method = RequestMethod.GET) String index(Model model, HttpServletRequest request); @RequestMapping(value = "defaultLoginPage.html", method = RequestMethod.GET) String roleDispatcher(Model model, HttpServletRequest request); @RequestMapping(value = "error.html") String error(HttpServletRequest request, Model model);  }
@Test public void testIndex() throws Exception { AuthenticatedUser currentUser = mock(AuthenticatedUser.class); when(userContext.getCurrentUser()).thenReturn(currentUser); when(currentUser.getUsername()).thenReturn("username"); Users users= mock(Users.class); when(usersRepository.loadUserByUsername("username")).thenReturn(users); Set<GrantedAuthority> auths=new HashSet<GrantedAuthority>(); auths.add(new SimpleGrantedAuthority("ROLE_USER")); when(users.getAuthorities()).thenReturn(auths); mockMvc.perform(get("/index.html")) .andExpect(view().name("redirect:/patients/home.html")); }