@Test @DisplayName("顧客の追加ができてindex画面にリダイレクトする") @Transactional public void insertCompleteTest2() { CustomerForm customerForm = new CustomerForm("絵梨花", "生田", "eikuta@nogi.com", LocalDate.of(1997, 1, 22)); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(customerForm, "customerForm"); String logicalViewName = customerController.insertComplete(customerForm, bindingResult); int rows = JdbcTestUtils.countRowsInTableWhere(jdbcTemplate, "customer", "email = '" + customerForm.getEmail() + "'"); assertAll( () -> assertEquals(1, rows), () -> assertEquals("redirect:/", logicalViewName) ); }
@PostMapping("/insertComplete") public String insertComplete( @Validated CustomerForm customerForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "insertMain"; } Customer customer = customerForm.convertToEntity(); customerService.save(customer); return "redirect:/"; }
CustomerController { @PostMapping("/insertComplete") public String insertComplete( @Validated CustomerForm customerForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "insertMain"; } Customer customer = customerForm.convertToEntity(); customerService.save(customer); return "redirect:/"; } }
CustomerController { @PostMapping("/insertComplete") public String insertComplete( @Validated CustomerForm customerForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "insertMain"; } Customer customer = customerForm.convertToEntity(); customerService.save(customer); return "redirect:/"; } CustomerController(CustomerService customerService); }
CustomerController { @PostMapping("/insertComplete") public String insertComplete( @Validated CustomerForm customerForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "insertMain"; } Customer customer = customerForm.convertToEntity(); customerService.save(customer); return "redirect:/"; } CustomerController(CustomerService customerService); @GetMapping("/") String index(Model model); @GetMapping("/insertMain") String insertMain(Model model); @PostMapping("/insertComplete") String insertComplete(
            @Validated CustomerForm customerForm,
            BindingResult bindingResult); }
CustomerController { @PostMapping("/insertComplete") public String insertComplete( @Validated CustomerForm customerForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "insertMain"; } Customer customer = customerForm.convertToEntity(); customerService.save(customer); return "redirect:/"; } CustomerController(CustomerService customerService); @GetMapping("/") String index(Model model); @GetMapping("/insertMain") String insertMain(Model model); @PostMapping("/insertComplete") String insertComplete(
            @Validated CustomerForm customerForm,
            BindingResult bindingResult);  }