LoginController { @GetMapping("/dashboard") public ModelAndView dashboard() { ModelAndView modelAndView = new ModelAndView(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Optional<User> user = this.userService.findUserByEmail(auth.getName()); modelAndView.addObject("currentUser", user.get()); modelAndView.addObject("fullName", "Welcome " + user.get().getFullname()); modelAndView.addObject("adminMessage", "Content Available Only for Users with Admin Role"); modelAndView.setViewName("dashboard"); return modelAndView; } LoginController(CustomUserDetailsServiceImpl userService); @GetMapping("/login") ModelAndView login(); @GetMapping("/signup") ModelAndView signup(); @PostMapping("/signup") ModelAndView createNewUser(@Valid @ModelAttribute User user, BindingResult bindingResult); @GetMapping("/dashboard") ModelAndView dashboard(); @GetMapping({ "/", "/home" }) ModelAndView home();  }
@Test @WithMockUser(authorities = "ADMIN", username = "junitAdmin@email.com") void testDashboard() throws Exception { given(this.userService.findUserByEmail("junitAdmin@email.com")) .willReturn(Optional.of(MockObjectCreator.getUser())); this.mockMvc.perform(get("/dashboard")).andExpect(status().isOk()).andExpect(view().name("dashboard")) .andExpect(model().attributeExists("currentUser")) .andExpect(model().attribute("fullName", "Welcome junitFullName")) .andExpect(model().attribute("adminMessage", "Content Available Only for Users with Admin Role")) .andExpect(model().errorCount(0)).andExpect(content().contentType("text/html;charset=UTF-8")); }