### Question:
CustomerController { public String insertComplete( CustomerForm customerForm, BindingResult bindingResult) { Customer customer = customerForm.convertToEntity(); return "redirect:/"; }  String index(Model model); @GetMapping("/insertMain") String insertMain(Model model); String insertComplete(
            // Bean Validationを実行するアノテーションを付加する
               CustomerForm customerForm,
            BindingResult bindingResult);  }### Answer:
@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) ); }
@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) ); }