Skip to content

Commit 223ecf2

Browse files
committed
重构springboot-restful工程
1 parent cdc08a0 commit 223ecf2

File tree

7 files changed

+238
-100
lines changed

7 files changed

+238
-100
lines changed

springboot-restful/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-jetty</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.hamcrest</groupId>
50+
<artifactId>hamcrest-all</artifactId>
51+
<version>1.3</version>
52+
<scope>test</scope>
53+
</dependency>
4354
</dependencies>
4455

4556
<build>

springboot-restful/src/main/java/com/xncoding/pos/controller/LoginController.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

springboot-restful/src/main/java/com/xncoding/pos/model/BaseResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.xncoding.pos.model;
22

33
/**
4-
* Controller的基础返回类
4+
* 基础返回类
55
*
66
* @author XiongNeng
77
* @version 1.0

springboot-restful/src/main/java/com/xncoding/pos/model/LoginParam.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.xncoding.pos.model;
2+
3+
/**
4+
* 用户
5+
*
6+
* @author XiongNeng
7+
* @version 1.0
8+
* @since 2018/3/4
9+
*/
10+
public class User {
11+
private Long id;
12+
private String name;
13+
private Integer age;
14+
15+
public User() {
16+
}
17+
18+
public User(Long id, String name, Integer age) {
19+
this.id = id;
20+
this.name = name;
21+
this.age = age;
22+
}
23+
24+
public Long getId() {
25+
return id;
26+
}
27+
28+
public void setId(Long id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Integer getAge() {
41+
return age;
42+
}
43+
44+
public void setAge(Integer age) {
45+
this.age = age;
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.xncoding.pos.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* JacksonUtil
11+
*
12+
* @author XiongNeng
13+
* @version 1.0
14+
* @since 2018/3/4
15+
*/
16+
public class JacksonUtil {
17+
private static ObjectMapper mapper = new ObjectMapper();
18+
19+
public static String bean2Json(Object obj) {
20+
try {
21+
return mapper.writeValueAsString(obj);
22+
} catch (JsonProcessingException e) {
23+
e.printStackTrace();
24+
return null;
25+
}
26+
}
27+
28+
// public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
29+
// try {
30+
// return mapper.readValue(jsonStr, objClass);
31+
// } catch (IOException e) {
32+
// e.printStackTrace();
33+
// return null;
34+
// }
35+
// }
36+
37+
public static <T> T json2Bean(String jsonStr, TypeReference<T> typeReference) {
38+
try {
39+
return mapper.readValue(jsonStr, typeReference);
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
return null;
43+
}
44+
}
45+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.xncoding.pos;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.xncoding.pos.model.BaseResponse;
5+
import com.xncoding.pos.model.User;
6+
import com.xncoding.pos.util.JacksonUtil;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.MvcResult;
15+
import org.springframework.test.web.servlet.RequestBuilder;
16+
17+
import java.util.Collections;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import static org.hamcrest.Matchers.*;
23+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29+
30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
32+
/**
33+
* ApplicationTests
34+
*
35+
* @author XiongNeng
36+
* @version 1.0
37+
* @since 2018/3/4
38+
*/
39+
40+
@AutoConfigureMockMvc
41+
@RunWith(SpringRunner.class)
42+
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
43+
public class ApplicationTests {
44+
@Autowired
45+
private MockMvc mvc;
46+
47+
@Test
48+
public void testUserController() throws Exception {
49+
// 测试UserController
50+
RequestBuilder request;
51+
52+
// 1、get查一下user列表,应该为空
53+
request = get("/users/");
54+
MvcResult result = mvc.perform(request)
55+
.andExpect(status().isOk())
56+
.andReturn();
57+
String content = result.getResponse().getContentAsString();
58+
BaseResponse<List<User>> response = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
59+
assertThat(response.isSuccess(), is(true));
60+
assertThat(response.getMsg(), is("查询列表成功"));
61+
assertThat(((List) response.getData()).size(), is(0));
62+
63+
// 2、post提交一个user
64+
request = post("/users/")
65+
.param("id", "1")
66+
.param("name", "测试大师")
67+
.param("age", "20");
68+
result = mvc.perform(request)
69+
.andExpect(status().isOk())
70+
.andReturn();
71+
content = result.getResponse().getContentAsString();
72+
BaseResponse<String> response1 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
73+
assertThat(response1.isSuccess(), is(true));
74+
assertThat(response1.getMsg(), is("新增成功"));
75+
76+
// 3、get获取user列表,应该有刚才插入的数据
77+
request = get("/users/");
78+
result = mvc.perform(request)
79+
.andExpect(status().isOk())
80+
.andReturn();
81+
content = result.getResponse().getContentAsString();
82+
BaseResponse<List<User>> response2 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
83+
assertThat(response2.isSuccess(), is(true));
84+
assertThat(response2.getMsg(), is("查询列表成功"));
85+
assertThat((response2.getData()).size(), is(1));
86+
87+
// 4、put修改id为1的user
88+
request = put("/users/1")
89+
.param("name", "测试终极大师")
90+
.param("age", "30");
91+
result = mvc.perform(request)
92+
.andExpect(status().isOk())
93+
.andReturn();
94+
content = result.getResponse().getContentAsString();
95+
BaseResponse<String> response3 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
96+
assertThat(response3.isSuccess(), is(true));
97+
assertThat(response3.getMsg(), is("更新成功"));
98+
99+
// 5、get一个id为1的user
100+
request = get("/users/1");
101+
result = mvc.perform(request)
102+
.andExpect(status().isOk())
103+
.andReturn();
104+
content = result.getResponse().getContentAsString();
105+
BaseResponse<User> response4 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<User>>() {});
106+
assertThat(response4.isSuccess(), is(true));
107+
assertThat(response4.getMsg(), is("查询成功"));
108+
User user = response4.getData();
109+
assertThat(user.getId(), is(1L));
110+
assertThat(user.getName(), is("测试终极大师"));
111+
112+
// 6、del删除id为1的user
113+
request = delete("/users/1");
114+
result = mvc.perform(request)
115+
.andExpect(status().isOk())
116+
.andReturn();
117+
content = result.getResponse().getContentAsString();
118+
BaseResponse<String> response5 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
119+
assertThat(response5.isSuccess(), is(true));
120+
assertThat(response5.getMsg(), is("删除成功"));
121+
122+
// 7、get查一下user列表,应该为空
123+
request = get("/users/");
124+
result = mvc.perform(request)
125+
.andExpect(status().isOk())
126+
.andReturn();
127+
content = result.getResponse().getContentAsString();
128+
BaseResponse<List<User>> response6 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
129+
assertThat(response6.isSuccess(), is(true));
130+
assertThat(response6.getMsg(), is("查询列表成功"));
131+
assertThat((response6.getData()).size(), is(0));
132+
}
133+
134+
}

0 commit comments

Comments
 (0)