### Question:
CodeReviewController { @RequestMapping(value = "/reviews/new", method = RequestMethod.POST) public ModelAndView createCodeReview(@Valid @ModelAttribute final TopicDto topicDto, BindingResult result, @RequestParam(BRANCH_ID) Long branchId) throws NotFoundException { Branch branch = branchService.get(branchId); final Topic topic = topicDto.getTopic(); topic.setBranch(branch); topic.setType(TopicTypeName.CODE_REVIEW.getName()); if (result.hasErrors()) { return new ModelAndView(CODE_REVIEW_VIEW) .addObject(TOPIC_DTO, topicDto) .addObject(BRANCH_ID, branchId) .addObject(SUBMIT_URL, "/reviews/new?branchId=" + branchId) .addObject(BREADCRUMB_LIST, breadcrumbBuilder.getNewTopicBreadcrumb(branch)); } Topic createdTopic = retryTemplate.execute(new RetryCallback<Topic, NotFoundException>() { @Override public Topic doWithRetry(RetryContext context) throws NotFoundException { return topicModificationService.createTopic(topic, topicDto.getBodyText()); } }); return new ModelAndView(REDIRECT_URL + createdTopic.getId()); } @Autowired  CodeReviewController(BranchService branchService,
                                BreadcrumbBuilder breadcrumbBuilder,
                                TopicModificationService topicModificationService,
                                TopicDraftService topicDraftService,
                                RetryTemplate retryTemplate); @RequestMapping(value = "/reviews/new", method = RequestMethod.GET) ModelAndView showNewCodeReviewPage(@RequestParam(BRANCH_ID) Long branchId); @RequestMapping(value = "/reviews/new", method = RequestMethod.POST) ModelAndView createCodeReview(@Valid @ModelAttribute final TopicDto topicDto,
                                         BindingResult result,
                                         @RequestParam(BRANCH_ID) Long branchId); static final String BRANCH_ID; static final String BREADCRUMB_LIST; }### Answer:
@Test public void createValidationPass() throws Exception { user.setAutosubscribe(true); Branch branch = createBranch(); Topic topic = createTopic(); TopicDto dto = getDto(); BindingResult result = mock(BindingResult.class); when(branchService.get(BRANCH_ID)).thenReturn(branch); when(topicModificationService.createTopic(topic, TOPIC_CONTENT)) .thenReturn(topic); ModelAndView mav = controller.createCodeReview(dto, result, BRANCH_ID); verify(topicModificationService).createTopic(topic, TOPIC_CONTENT); assertViewName(mav, "redirect:/topics/1"); }
@Test public void createValidationFail() throws Exception { BindingResult result = mock(BindingResult.class); when(result.hasErrors()).thenReturn(true); when(branchService.get(BRANCH_ID)).thenReturn(branch); List<Breadcrumb> breadcrumbs = new BreadcrumbBuilder().getNewTopicBreadcrumb(branch); when(breadcrumbBuilder.getNewTopicBreadcrumb(branch)).thenReturn(breadcrumbs); ModelAndView mav = controller.createCodeReview(getDto(), result, BRANCH_ID); List<Breadcrumb> breadcrumbsFromModel = (List) mav.getModel().get("breadcrumbList"); for (int i = 0; i < breadcrumbs.size(); i++) { assertEquals(breadcrumbs.get(i).getValue(), breadcrumbsFromModel.get(i).getValue()); } assertEquals(breadcrumbsFromModel.get(1).getValue(), branch.getSection().getName()); assertEquals(breadcrumbsFromModel.get(2).getValue(), branch.getName()); assertViewName(mav, "codeReviewForm"); long branchId = assertAndReturnModelAttributeOfType(mav, "branchId", Long.class); assertEquals(branchId, BRANCH_ID); }