@Test @DisplayName("検証エラー時にinsertMain画面に戻る") public void insertCompleteTest1() { Method indexMethod = assertDoesNotThrow(() -> CustomerController.class.getMethod("insertComplete", CustomerForm.class, BindingResult.class)); PostMapping postMapping = indexMethod.getAnnotation(PostMapping.class); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(CustomerForm.createEmptyForm(), "customerForm") { @Override public boolean hasErrors() { return true; } }; String logicalViewName = customerController.insertComplete(CustomerForm.createEmptyForm(), bindingResult); assertAll( () -> assertNotNull(postMapping), () -> assertEquals("/insertComplete", postMapping.value()[0]), () -> assertEquals("insertMain", 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);  }