### Question:
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; }### Answer:
@Test public void testAddSpanner() { SpannerForm formData = new SpannerForm(); formData.setName("Keeley"); formData.setSize(12); Principal currentUser = new TestingAuthenticationToken("user", "pass"); BindingResult noErrors = new BeanPropertyBindingResult(formData, MODEL_SPANNER); ModelAndView response = controller.addSpanner(formData, noErrors, currentUser); assertNotNull("no response", response); ArgumentCaptor<Spanner> spannerArgumentCaptor = ArgumentCaptor.forClass(Spanner.class); verify(spannersService).create(spannerArgumentCaptor.capture()); Spanner createdSpanner = spannerArgumentCaptor.getValue(); assertEquals("name", formData.getName(), createdSpanner.getName()); assertEquals("size", formData.getSize(), createdSpanner.getSize()); assertEquals("owner", currentUser.getName(), createdSpanner.getOwner()); assertEquals("view", VIEW_SUCCESS, response.getViewName()); }
@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()); }