ConfigurationFormController { @RequestMapping(method = POST) public String onFormSubmit(@ModelAttribute("command") @Valid ConfigCommand command, BindingResult result, Model model) { if (result.hasErrors()) { return "config/configForm"; } logger.debug("Adding configuration from command: " + command); final RepositoryConfiguration repositoryConfiguration = command.createRepositoryConfiguration(); application.addConfiguration(repositoryConfiguration); model.addAttribute("addedRepositories", application.getRepositoryNames()); model.addAttribute("latestAddedRepository", command.getName()); return "config/listConfigs"; } @Autowired  ConfigurationFormController(final Application application); @Autowired void setValidator(ConfigCommandValidator validator); @InitBinder("command") void bindValidator(WebDataBinder binder); @RequestMapping(method = GET) ModelAndView setUpForm(ModelAndView modelAndView); @RequestMapping(method = POST) String onFormSubmit(@ModelAttribute("command") @Valid ConfigCommand command, BindingResult result, Model model);  }
@Test public void onFormSubmitRedirectsToFormOnValidationError() { Application application = mock(Application.class); ConfigurationFormController controller = new ConfigurationFormController(application); BindingResult bindingResult = mock(BindingResult.class); when(bindingResult.hasErrors()).thenReturn(true); String formView = controller.onFormSubmit(new ConfigCommand(), bindingResult, mock(Model.class)); assertThat(formView, is(CONFIG_FORM_VIEW_NAME)); }
@Test public void onFormSubmitRepositoryAdded() throws Exception { Application application = mock(Application.class); HashSet<RepositoryName> repositoryNames = new HashSet<RepositoryName>(); repositoryNames.add(new RepositoryName("sventon")); repositoryNames.add(new RepositoryName("nordpolen")); when(application.getRepositoryNames()).thenReturn(repositoryNames); ConfigCommand command = new ConfigCommand(); command.setName("nordpolen"); command.setRepositoryUrl("svn: BindingResult bindingResult = mock(BindingResult.class); when(bindingResult.hasErrors()).thenReturn(false); Model model = new ExtendedModelMap(); ConfigurationFormController controller = new ConfigurationFormController(application); String listConfigView = controller.onFormSubmit(command, bindingResult, model); Map expectedModel = new HashMap(); expectedModel.put("addedRepositories", repositoryNames); expectedModel.put("latestAddedRepository", command.getName()); assertThat(listConfigView, is(LIST_CONFIGS_VIEW_NAME)); assertThat(model.asMap(), is(expectedModel)); }