@Test public void testReserveTourReserveFailByBusinessException() { MockHttpServletRequestBuilder postRequest = MockMvcRequestBuilders.post( "/tours/123/reserve").with(user(userDetails)); when(reserveTourHelper.reserve(any(ReservationUserDetails.class), eq( "123"), any(ReserveTourForm.class))).thenThrow( new BusinessException("")); when(reserveTourHelper.findTourDetail(any(ReservationUserDetails.class), eq("123"), any(ReserveTourForm.class))).thenReturn( new TourDetailOutput()); postRequest.param("adultCount", "2"); postRequest.param("childCount", "2"); try { ResultActions results = mockMvc.perform(postRequest); results.andExpect(status().isOk()); results.andExpect(model().hasNoErrors()); results.andExpect(model().attribute("output", notNullValue())); results.andExpect(model().attribute("resultMessages", notNullValue())); results.andExpect(view().name("reservetour/reserveForm")); return; } catch (Exception e) { e.printStackTrace(); } fail(); }
@TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) public String reserve( @AuthenticationPrincipal ReservationUserDetails userDetails, @PathVariable("tourCode") String tourCode, @Validated ReserveTourForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttr) { log.debug("reserve tour {}", tourCode); if (bindingResult.hasErrors()) { return reserveForm(userDetails, tourCode, form, model); } try { ReserveTourOutput output = reserveTourHelper.reserve(userDetails, tourCode, form); redirectAttr.addFlashAttribute("output", output); } catch (BusinessException e) { model.addAttribute(e.getResultMessages()); return reserveForm(userDetails, tourCode, form, model); } return "redirect:/tours/{tourCode}/reserve?complete"; }
ReserveTourController { @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) public String reserve( @AuthenticationPrincipal ReservationUserDetails userDetails, @PathVariable("tourCode") String tourCode, @Validated ReserveTourForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttr) { log.debug("reserve tour {}", tourCode); if (bindingResult.hasErrors()) { return reserveForm(userDetails, tourCode, form, model); } try { ReserveTourOutput output = reserveTourHelper.reserve(userDetails, tourCode, form); redirectAttr.addFlashAttribute("output", output); } catch (BusinessException e) { model.addAttribute(e.getResultMessages()); return reserveForm(userDetails, tourCode, form, model); } return "redirect:/tours/{tourCode}/reserve?complete"; } }
ReserveTourController { @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) public String reserve( @AuthenticationPrincipal ReservationUserDetails userDetails, @PathVariable("tourCode") String tourCode, @Validated ReserveTourForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttr) { log.debug("reserve tour {}", tourCode); if (bindingResult.hasErrors()) { return reserveForm(userDetails, tourCode, form, model); } try { ReserveTourOutput output = reserveTourHelper.reserve(userDetails, tourCode, form); redirectAttr.addFlashAttribute("output", output); } catch (BusinessException e) { model.addAttribute(e.getResultMessages()); return reserveForm(userDetails, tourCode, form, model); } return "redirect:/tours/{tourCode}/reserve?complete"; }  }
ReserveTourController { @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) public String reserve( @AuthenticationPrincipal ReservationUserDetails userDetails, @PathVariable("tourCode") String tourCode, @Validated ReserveTourForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttr) { log.debug("reserve tour {}", tourCode); if (bindingResult.hasErrors()) { return reserveForm(userDetails, tourCode, form, model); } try { ReserveTourOutput output = reserveTourHelper.reserve(userDetails, tourCode, form); redirectAttr.addFlashAttribute("output", output); } catch (BusinessException e) { model.addAttribute(e.getResultMessages()); return reserveForm(userDetails, tourCode, form, model); } return "redirect:/tours/{tourCode}/reserve?complete"; }  @ModelAttribute ReserveTourForm setUpReserveTourForm(); @RequestMapping(value = { "{tourCode}", "{tourCode}/reserve" }, method = RequestMethod.GET, params = "form") String reserveForm(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode, ReserveTourForm form,
            Model model); @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.BEGIN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST, params = "confirm") String confirm(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode,
            @Validated ReserveTourForm form, BindingResult bindingResult,
            Model model); @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) String reserve(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode,
            @Validated ReserveTourForm form, BindingResult bindingResult,
            Model model, RedirectAttributes redirectAttr); @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.GET, params = "complete") String reserveComplete(); @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST, params = "redo") String reserveRedo(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode, ReserveTourForm form,
            Model model); }
ReserveTourController { @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) public String reserve( @AuthenticationPrincipal ReservationUserDetails userDetails, @PathVariable("tourCode") String tourCode, @Validated ReserveTourForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttr) { log.debug("reserve tour {}", tourCode); if (bindingResult.hasErrors()) { return reserveForm(userDetails, tourCode, form, model); } try { ReserveTourOutput output = reserveTourHelper.reserve(userDetails, tourCode, form); redirectAttr.addFlashAttribute("output", output); } catch (BusinessException e) { model.addAttribute(e.getResultMessages()); return reserveForm(userDetails, tourCode, form, model); } return "redirect:/tours/{tourCode}/reserve?complete"; }  @ModelAttribute ReserveTourForm setUpReserveTourForm(); @RequestMapping(value = { "{tourCode}", "{tourCode}/reserve" }, method = RequestMethod.GET, params = "form") String reserveForm(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode, ReserveTourForm form,
            Model model); @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.BEGIN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST, params = "confirm") String confirm(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode,
            @Validated ReserveTourForm form, BindingResult bindingResult,
            Model model); @TransactionTokenCheck(value = "reserve", type = TransactionTokenType.IN) @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST) String reserve(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode,
            @Validated ReserveTourForm form, BindingResult bindingResult,
            Model model, RedirectAttributes redirectAttr); @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.GET, params = "complete") String reserveComplete(); @RequestMapping(value = "{tourCode}/reserve", method = RequestMethod.POST, params = "redo") String reserveRedo(
            @AuthenticationPrincipal ReservationUserDetails userDetails,
            @PathVariable("tourCode") String tourCode, ReserveTourForm form,
            Model model);  }