diff --git a/chapter-4-spring-boot-validating-form-input/pom.xml b/chapter-4-spring-boot-validating-form-input/pom.xml
index 2c63471..ef6e623 100644
--- a/chapter-4-spring-boot-validating-form-input/pom.xml
+++ b/chapter-4-spring-boot-validating-form-input/pom.xml
@@ -54,7 +54,6 @@
com.h2database
h2
- runtime
diff --git a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/ValidatingFormInputApplication.java b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/ValidatingFormInputApplication.java
index 1825f04..c6cb5ac 100644
--- a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/ValidatingFormInputApplication.java
+++ b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/ValidatingFormInputApplication.java
@@ -1,12 +1,40 @@
package spring.boot.core;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import spring.boot.core.domain.User;
+import spring.boot.core.domain.UserRepository;
@SpringBootApplication
-public class ValidatingFormInputApplication {
+public class ValidatingFormInputApplication implements CommandLineRunner {
- public static void main(String[] args) {
- SpringApplication.run(ValidatingFormInputApplication.class, args);
- }
+
+ private Logger LOG = LoggerFactory.getLogger(ValidatingFormInputApplication.class);
+
+ @Autowired
+ private UserRepository userRepository;
+
+ public static void main(String[] args) {
+ SpringApplication.run(ValidatingFormInputApplication.class, args);
+ }
+
+ @Override
+ public void run(String... args) throws Exception {
+ User user1 = new User("Sergey", 24, "1994-01-01");
+ User user2 = new User("Ivan", 26, "1994-01-01");
+ User user3 = new User("Adam", 31, "1994-01-01");
+ LOG.info("Inserting data in DB.");
+ userRepository.save(user1);
+ userRepository.save(user2);
+ userRepository.save(user3);
+ LOG.info("User count in DB: {}", userRepository.count());
+ LOG.info("User with ID 1: {}", userRepository.findById(1L));
+ LOG.info("Deleting user with ID 2L form DB.");
+ userRepository.deleteById(2L);
+ LOG.info("User count in DB: {}", userRepository.count());
+ }
}
diff --git a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/User.java b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/User.java
index 535d3a5..45d4aaf 100644
--- a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/User.java
+++ b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/domain/User.java
@@ -79,6 +79,15 @@ public class User implements Serializable {
this.birthday = birthday;
}
+
+ public User(String name, Integer age, String birthday) {
+ this.name = name;
+ this.age = age;
+ this.birthday = birthday;
+ }
+
+ public User() {}
+
@Override
public String toString() {
return "User{" +
diff --git a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/web/UserController.java b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/web/UserController.java
index 3eb0360..8a51c84 100644
--- a/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/web/UserController.java
+++ b/chapter-4-spring-boot-validating-form-input/src/main/java/spring/boot/core/web/UserController.java
@@ -103,7 +103,7 @@ public class UserController {
/**
* 处理 "/users/{id}" 的 GET 请求,用来删除 User 信息
*/
- @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
+ @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
userService.delete(id);
diff --git a/chapter-4-spring-boot-validating-form-input/src/main/resources/application.properties b/chapter-4-spring-boot-validating-form-input/src/main/resources/application.properties
index 51350c6..1a47991 100644
--- a/chapter-4-spring-boot-validating-form-input/src/main/resources/application.properties
+++ b/chapter-4-spring-boot-validating-form-input/src/main/resources/application.properties
@@ -1,2 +1,15 @@
+## 开启 H2 数据库
+spring.h2.console.enabled=true
+
+## 配置 H2 数据库连接信息
+spring.datasource.url=jdbc:h2:mem:testdb
+spring.datasource.driverClassName=org.h2.Driver
+spring.datasource.username=sa
+spring.datasource.password=
+
+
+
## 是否显示 SQL 语句
-spring.jpa.show-sql=true
\ No newline at end of file
+spring.jpa.show-sql=true
+hibernate.dialect=org.hibernate.dialect.H2Dialect
+hibernate.hbm2ddl.auto=create
diff --git a/chapter-4-spring-boot-validating-form-input/src/test/java/spring/boot/core/web/UserControllerTest.java b/chapter-4-spring-boot-validating-form-input/src/test/java/spring/boot/core/web/UserControllerTest.java
new file mode 100644
index 0000000..08e9af7
--- /dev/null
+++ b/chapter-4-spring-boot-validating-form-input/src/test/java/spring/boot/core/web/UserControllerTest.java
@@ -0,0 +1,159 @@
+package spring.boot.core.web;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
+import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import spring.boot.core.ValidatingFormInputApplication;
+import spring.boot.core.domain.User;
+import spring.boot.core.service.UserService;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
+import static org.junit.Assert.assertEquals;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ValidatingFormInputApplication.class)
+@AutoConfigureMockMvc
+@TestPropertySource(locations = "classpath:application.properties")
+public class UserControllerTest {
+
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Autowired
+ private UserService userService;
+
+ @Autowired
+ private ObjectMapper objectMapper;
+
+ @Test
+ public void getUserList() throws Exception {
+ mockMvc.perform(get("/users"))
+ .andExpect(view().name("userList"))
+ .andExpect(status().isOk())
+ .andDo(print());
+ }
+
+ private User createUser() {
+ User user = new User();
+ user.setName("测试用户");
+ user.setAge(100);
+ user.setBirthday("1994-01-01");
+ return userService.insertByUser(user);
+ }
+
+ @Test
+ public void createUserForm() throws Exception {
+
+ mockMvc.perform(get("/users/create"))
+ .andDo(print())
+ .andExpect(view().name("userForm"))
+ .andExpect(request().attribute("action", "create"))
+ .andDo(print())
+ .andReturn();
+ }
+
+ @Test
+ public void postUser() throws Exception {
+ User user = createUser();
+ assertNotNull(user);
+
+ MultiValueMap parameters = new LinkedMultiValueMap();
+ Map maps = objectMapper.convertValue(user, new TypeReference