SpringBoot 构建RESTful API与单元测试

不需要启动服务器即可测试我们的RESTful

RESTful API

构建项目

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import lombok.Data;

/**
* @author Ray
* @date 2018/7/21 0021
*/
@Data
public class User {

private Long id;
private String name;
private Integer age;

}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @author Ray
* @date 2018/7/21 0021
*/
@RestController
@RequestMapping(value = "/users") // 所有映射都在/users下
public class UserController {

/**
* 创建一个容器
*/
static Map<Long,User> users = Collections.synchronizedMap(new HashMap<>());

/**
* 处理"/users/" 的GET请求,用来获取用户列表
* 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<User> getUserList(){
List<User> list = new ArrayList<>(users.values());
return list;
}

/**
* 处理"/users/" 的POST请求,用来创建User
* 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
*/
@RequestMapping(value = "/", method = RequestMethod.POST)
public String postUser(@ModelAttribute User user){
users.put(user.getId(), user);
return "success";
}

/**
* 处理"/users/{id}" 的GET请求,用来获取url中id值得User信息
* url中的id可通过@PathVariable绑定到函数的参数中
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id){
return users.get(id);
}

/**
* 处理"/users/{id}" 的PUT请求, 用来更新User信息
*/
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String putUser(@PathVariable Long id, @ModelAttribute User user){
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";

}

/**
* 处理"/users/{id}" 的DELETE请求,用来删除User
*/
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id){
users.remove(id);
return "success";
}
}

测试项目

注意

注意:包的引用

1
2
3
4
import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @author Ray
* @date 2018/7/21 0021
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {

private MockMvc mvc;

@Before
public void setUp(){
mvc = MockMvcBuilders.standaloneSetup(
new UserController()
).build();
}

/**
* 测试UserController
*/
@Test
public void test() throws Exception {
RequestBuilder request = null;

// 1.get查一下user列表,应该为空 []
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[]")));

// 2.post提交一个user
request = post("/users/")
.param("id", "1")
.param("name", "测试")
.param("age", "20");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));

// 3.get获取user列表,应该有刚才插入的数据
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[{\"id\":1,\"name\":\"测试\",\"age\":20}]")));

// 4.put修改id为1的user
request = put("/users/1")
.param("name", "测试put")
.param("age", "30");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));

// 5.get一个id为1的user
request = get("/users/1");
mvc.perform(request)
.andExpect(content().string(equalTo("{\"id\":1,\"name\":\"测试put\",\"age\":30}")));

// 6.del删除id为1的user
request = delete("/users/1");
mvc.perform(request)
.andExpect(content().string(equalTo("success")));

// 7.get查一下user列表,应该为空 []
request = get("/users/");
mvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[]")));
}
}
-------------- 本文结束  感谢您的阅读 --------------