@Test public void testSendEmail() throws Exception { String from = "test@mail.com"; String message = "Mail message"; String name = "Name"; ContactForm contactForm = new ContactForm(); contactForm.setMail(from); contactForm.setMessage(message); contactForm.setName(name); Mockito.when(mailService.sendEMail(from, mailTo, "Message from " + name, message)).thenReturn(true); mockMvc.perform(post("/contact").accept(MediaType.APPLICATION_XHTML_XML) .sessionAttr("contactForm", contactForm)) .andExpect(redirectedUrl("contact?sent=false")) .andExpect(model().attributeExists("contactForm")); }
@RequestMapping(method = RequestMethod.POST) public String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results) { if (results.hasErrors()) { return "contact"; } String name = contactForm.getName(); String from = contactForm.getMail(); String message = contactForm.getMessage(); boolean emailSent = mailService.sendEMail(from, mailTo, "Message from " + name, message); return "redirect:contact?sent=" + emailSent; }
ContactController { @RequestMapping(method = RequestMethod.POST) public String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results) { if (results.hasErrors()) { return "contact"; } String name = contactForm.getName(); String from = contactForm.getMail(); String message = contactForm.getMessage(); boolean emailSent = mailService.sendEMail(from, mailTo, "Message from " + name, message); return "redirect:contact?sent=" + emailSent; } }
ContactController { @RequestMapping(method = RequestMethod.POST) public String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results) { if (results.hasErrors()) { return "contact"; } String name = contactForm.getName(); String from = contactForm.getMail(); String message = contactForm.getMessage(); boolean emailSent = mailService.sendEMail(from, mailTo, "Message from " + name, message); return "redirect:contact?sent=" + emailSent; } @Autowired  ContactController(IMailService mailService); }
ContactController { @RequestMapping(method = RequestMethod.POST) public String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results) { if (results.hasErrors()) { return "contact"; } String name = contactForm.getName(); String from = contactForm.getMail(); String message = contactForm.getMessage(); boolean emailSent = mailService.sendEMail(from, mailTo, "Message from " + name, message); return "redirect:contact?sent=" + emailSent; } @Autowired  ContactController(IMailService mailService); @RequestMapping(method = RequestMethod.GET) String showContactForm(Model model); @RequestMapping(method = RequestMethod.POST) String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results); }
ContactController { @RequestMapping(method = RequestMethod.POST) public String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results) { if (results.hasErrors()) { return "contact"; } String name = contactForm.getName(); String from = contactForm.getMail(); String message = contactForm.getMessage(); boolean emailSent = mailService.sendEMail(from, mailTo, "Message from " + name, message); return "redirect:contact?sent=" + emailSent; } @Autowired  ContactController(IMailService mailService); @RequestMapping(method = RequestMethod.GET) String showContactForm(Model model); @RequestMapping(method = RequestMethod.POST) String sendEmail(@Valid @ModelAttribute("contactForm") ContactForm contactForm, BindingResult results);  }