LoginController { @PostMapping("/signup") public ModelAndView createNewUser(@Valid @ModelAttribute User user, BindingResult bindingResult) { ModelAndView modelAndView = new ModelAndView(); Optional<User> userExists = this.userService.findUserByEmail(user.getEmail()); if (userExists.isPresent()) { bindingResult.rejectValue("email", "error.user", "There is already a user registered with the username provided"); } if (bindingResult.hasErrors()) { modelAndView.setViewName("signup"); } else { this.userService.saveUser(user); modelAndView.addObject("successMessage", "User has been registered successfully"); modelAndView.addObject("user", new User()); modelAndView.setViewName("login"); } 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 void testCreateNewUser() throws Exception { given(this.userService.findUserByEmail("junit@email.com")).willReturn(Optional.empty()); willDoNothing().given(this.userService).saveUser(MockObjectCreator.getUser()); this.mockMvc .perform(post("/signup").content(asJsonString(MockObjectCreator.getUser())) .contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isOk()).andExpect(view().name("login")).andExpect(model().attributeExists("user")) .andExpect(model().attribute("successMessage", "User has been registered successfully")) .andExpect(model().errorCount(0)).andExpect(content().contentType("text/html;charset=UTF-8")); }