@Test public void updateBindingResultErrors() { // Given Model model = new ExtendedModelMap(); givenPopulateModel(); Entry entry = entryFactoryForTest.newEntry(); BindingResult bindingResult = mock(BindingResult.class); when(bindingResult.hasErrors()).thenReturn(true); RedirectAttributes redirectAttributes = mock(RedirectAttributes.class); HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); // When String viewName = entryController.update(entry, bindingResult, model, redirectAttributes, httpServletRequest); // Then assertEquals("entry/form", viewName); Map<String, ?> modelMap = model.asMap(); assertEquals(entry, (Entry) modelMap.get("entry")); assertEquals("update", modelMap.get("mode")); assertEquals("/entry/update", modelMap.get("saveAction")); @SuppressWarnings("unchecked") List<EntrytypeListItem> entrytypeListItems = (List<EntrytypeListItem>) modelMap.get("listOfEntrytypeItems"); assertEquals(2, entrytypeListItems.size()); }
@Test public void updateOk() { // Given Model model = new ExtendedModelMap(); Entry entry = entryFactoryForTest.newEntry(); Integer identry = entry.getIdentry(); BindingResult bindingResult = mock(BindingResult.class); RedirectAttributes redirectAttributes = mock(RedirectAttributes.class); HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); Entry entrySaved = new Entry(); entrySaved.setIdentry(identry); when(entryService.update(entry)).thenReturn(entrySaved); // When String viewName = entryController.update(entry, bindingResult, model, redirectAttributes, httpServletRequest); // Then assertEquals("redirect:/entry/form/" + entry.getIdentry(), viewName); Map<String, ?> modelMap = model.asMap(); assertEquals(entrySaved, (Entry) modelMap.get("entry")); assertEquals(null, modelMap.get("mode")); assertEquals(null, modelMap.get("saveAction")); Mockito.verify(messageHelper) .addMessage(redirectAttributes, new Message(MessageType.SUCCESS, "save.ok")); }