### Question:
RadiologyReportFormController { @RequestMapping(method = RequestMethod.POST, params = "saveRadiologyReportDraft") protected ModelAndView saveRadiologyReportDraft(HttpServletRequest request, @ModelAttribute RadiologyReport radiologyReport) { final ModelAndView modelAndView = new ModelAndView(RADIOLOGY_REPORT_FORM_VIEW); try { radiologyReportService.saveRadiologyReportDraft(radiologyReport); request.getSession() .setAttribute(WebConstants.OPENMRS_MSG_ATTR, "radiology.RadiologyReport.savedDraft"); modelAndView.setViewName( "redirect:" + RADIOLOGY_REPORT_FORM_REQUEST_MAPPING + "?reportId=" + radiologyReport.getReportId()); return modelAndView; } catch (APIException apiException) { request.getSession() .setAttribute(WebConstants.OPENMRS_ERROR_ATTR, apiException.getMessage()); } addObjectsToModelAndView(modelAndView, radiologyReport); modelAndView.addObject(new VoidRadiologyReportRequest()); return modelAndView; }    }### Answer:
@Test public void shouldSaveGivenRadiologyReportAndSetHttpSessionAttributeOpenmrsMessageToReportDraftSavedAndRedirectToItsReportForm() { RadiologyReport mockRadiologyReport = RadiologyTestData.getMockRadiologyReport1(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.addParameter("saveRadiologyReportDraft", "saveRadiologyReportDraft"); MockHttpSession mockSession = new MockHttpSession(); mockRequest.setSession(mockSession); ModelAndView modelAndView = radiologyReportFormController.saveRadiologyReportDraft(mockRequest, mockRadiologyReport); verify(radiologyReportService, times(1)).saveRadiologyReportDraft(mockRadiologyReport); verifyNoMoreInteractions(radiologyReportService); assertNotNull(modelAndView); assertThat(modelAndView.getViewName(), is("redirect:/module/radiology/radiologyReport.form?reportId=" + mockRadiologyReport.getReportId())); assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_MSG_ATTR), is("radiology.RadiologyReport.savedDraft")); }
@Test public void shouldNotRedirectAndSetSessionAttributeWithOpenmrsErrorIfApiExceptionIsThrownBySaveRadiologyReportDraft() throws Exception { RadiologyReport mockRadiologyReport = RadiologyTestData.getMockRadiologyReport1(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(); mockRequest.addParameter("saveRadiologyReportDraft", "saveRadiologyReportDraft"); MockHttpSession mockSession = new MockHttpSession(); mockRequest.setSession(mockSession); when(radiologyReportService.saveRadiologyReportDraft(mockRadiologyReport)) .thenThrow(new APIException("RadiologyReport.cannot.saveDraft.already.reported")); ModelAndView modelAndView = radiologyReportFormController.saveRadiologyReportDraft(mockRequest, mockRadiologyReport); verify(radiologyReportService, times(1)).saveRadiologyReportDraft(mockRadiologyReport); verifyNoMoreInteractions(radiologyReportService); assertNotNull(modelAndView); assertThat(modelAndView.getViewName(), is(RadiologyReportFormController.RADIOLOGY_REPORT_FORM_VIEW)); assertThat(modelAndView.getModelMap(), hasKey("radiologyOrder")); RadiologyOrder radiologyOrder = (RadiologyOrder) modelAndView.getModelMap() .get("radiologyOrder"); assertThat(radiologyOrder, is(mockRadiologyReport.getRadiologyOrder())); RadiologyReport radiologyReport = (RadiologyReport) modelAndView.getModelMap() .get("radiologyReport"); assertNotNull(radiologyReport); assertThat(radiologyReport, is(mockRadiologyReport)); VoidRadiologyReportRequest voidRadiologyReportRequest = (VoidRadiologyReportRequest) modelAndView.getModelMap() .get("voidRadiologyReportRequest"); assertNotNull(voidRadiologyReportRequest); assertThat((String) mockSession.getAttribute(WebConstants.OPENMRS_ERROR_ATTR), is("RadiologyReport.cannot.saveDraft.already.reported")); }