@Test public void testValidationFail() { SpannerForm formData = new SpannerForm(); formData.setName("Keeley"); formData.setSize(12); BindingResult validationFail = new BeanPropertyBindingResult(formData, MODEL_SPANNER); validationFail.addError(new ObjectError("size", "TOO_BIG")); Principal currentUser = new TestingAuthenticationToken("user", "pass"); ModelAndView response = controller.addSpanner(formData, validationFail, currentUser); assertNotNull("no response", response); verify(spannersService, never()).update(any(Spanner.class)); assertEquals("view", VIEW_VALIDATION_FAIL, response.getViewName()); }
@RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal) { if (validationResult.hasErrors()) { return new ModelAndView(VIEW_VALIDATION_FAIL); } Spanner spanner = new Spanner(); spanner.setOwner(principal.getName()); spanner.setName(formData.getName()); spanner.setSize(formData.getSize()); spannersService.create(spanner); return new ModelAndView(VIEW_SUCCESS); }
AddSpannerController { @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal) { if (validationResult.hasErrors()) { return new ModelAndView(VIEW_VALIDATION_FAIL); } Spanner spanner = new Spanner(); spanner.setOwner(principal.getName()); spanner.setName(formData.getName()); spanner.setSize(formData.getSize()); spannersService.create(spanner); return new ModelAndView(VIEW_SUCCESS); } }
AddSpannerController { @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal) { if (validationResult.hasErrors()) { return new ModelAndView(VIEW_VALIDATION_FAIL); } Spanner spanner = new Spanner(); spanner.setOwner(principal.getName()); spanner.setName(formData.getName()); spanner.setSize(formData.getSize()); spannersService.create(spanner); return new ModelAndView(VIEW_SUCCESS); }  }
AddSpannerController { @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal) { if (validationResult.hasErrors()) { return new ModelAndView(VIEW_VALIDATION_FAIL); } Spanner spanner = new Spanner(); spanner.setOwner(principal.getName()); spanner.setName(formData.getName()); spanner.setSize(formData.getSize()); spannersService.create(spanner); return new ModelAndView(VIEW_SUCCESS); }  @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.GET) ModelAndView displayPage(); @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal); }
AddSpannerController { @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) public ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal) { if (validationResult.hasErrors()) { return new ModelAndView(VIEW_VALIDATION_FAIL); } Spanner spanner = new Spanner(); spanner.setOwner(principal.getName()); spanner.setName(formData.getName()); spanner.setSize(formData.getSize()); spannersService.create(spanner); return new ModelAndView(VIEW_SUCCESS); }  @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.GET) ModelAndView displayPage(); @RequestMapping(value = CONTROLLER_URL, method = RequestMethod.POST) ModelAndView addSpanner(@Valid @ModelAttribute(MODEL_SPANNER) SpannerForm formData, BindingResult validationResult, Principal principal); static final String CONTROLLER_URL; static final String VIEW_ADD_SPANNER; static final String VIEW_SUCCESS; static final String VIEW_VALIDATION_FAIL; static final String MODEL_SPANNER; }