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);  }
@Test public void testReserveTourReserveSuccess() { MockHttpServletRequestBuilder postRequest = MockMvcRequestBuilders.post( "/tours/123/reserve").with(user(userDetails)); when(reserveTourHelper.reserve(any(ReservationUserDetails.class), eq( "123"), any(ReserveTourForm.class))).thenReturn( new ReserveTourOutput()); postRequest.param("adultCount", "2"); postRequest.param("childCount", "2"); try { ResultActions results = mockMvc.perform(postRequest); results.andExpect(status().isFound()); results.andExpect(model().hasNoErrors()); results.andExpect(flash().attribute("output", notNullValue())); results.andExpect(view().name( "redirect:/tours/{tourCode}/reserve?complete")); return; } catch (Exception e) { e.printStackTrace(); } fail(); }
@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(); }