UnitTest单元测试

unitTestBase类,其他test类集成Base类

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import com.alibaba.fastjson.JSONObject;
import com.isstech.cay.model.result.ResultCode;
import com.isstech.cay.utils.AssertUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.status;


@RunWith(SpringRunner.class)
@SpringBootTest
public class AnalysisApplicationTests {

protected MockMvc mockMvc;

@Autowired
protected WebApplicationContext wac;

@Before
public void init() {
System.out.println("开始测试-----------------");
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
}

@Test
public void test() {
}

@After
public void after() {
System.out.println("测试结束-----------------");
}

/**
* 添加
*
* @param url
* @param entity
* @throws Exception
*/
public JSONObject testAdd(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "添加失败!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}

/**
* 修改
*
* @param url
* @param entity
* @throws Exception
*/
public JSONObject testUpdate(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "修改失败!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}


/**
* 删除
*
* @param url 删除地址
* @throws Exception
*/
public JSONObject testDel(String url) throws Exception {
String responseString = mockMvc.perform(delete(url)
.contentType(MediaType.APPLICATION_JSON)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "删除失败!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}

/**
* 查询
*
* @param url
* @return
* @throws Exception
*/
public JSONObject testGet(String url) throws Exception {
String responseString = mockMvc.perform(get(url)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "GET查询失败!");
return jsonObject;
}

/**
* 查询
*
* @param url
* @return
* @throws Exception
*/
public JSONObject testPost(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "POST查询失败!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}
}

api接口测试

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import com.isstech.cay.model.BpSysNotice;
import com.isstech.cay.model.BpTodo;
import org.junit.Test;

/**
* 待办事项管理
*
* @author Colin.Ye
* @version 1.0
* @ClassName BpSysTodoTests
* @date 2020/4/13
**/
public class BpSysTodoTests extends AnalysisApplicationTests {

private static final String REST = "/bp/todo/";

/**
* 新增通知
*/
@Test
@Transactional
@Rollback
public void add() throws Exception {
super.testAdd(REST + "add", new BpTodo() {{
// setId("5630e9f55cd94e8b878c6f4d672191ad");
setTitle("测试待办事项");
setSysId("系统ID");
setTodoType("待办事项类型");
setSysUrl("系统URL");
setCreatorId("createId");
setUpdateId("updateId");
}});
}

/**
* 修改通知
*/
@Test
@Transactional
@Rollback
public void update() throws Exception {
super.testUpdate(REST + "update", new BpTodo() {{
setId("5630e9f55cd94e8b878c6f4d672191ad");
setTitle("测试待办事项1");
setSysId("系统ID1");
setTodoType("待办事项类型1");
setSysUrl("系统URL1");
setCreatorId("createId1");
setUpdateId("updateId1");
}});
}

/**
* 删除通知
*
* @throws Exception
*/
@Test
@Transactional
@Rollback
public void del() throws Exception {
String id = "5630e9f55cd94e8b878c6f4d672191ad";
super.testDel(REST + "delete/" + id);
}

/**
* 根据ID查询通知信息
*
* @throws Exception
*/
@Test
public void get() throws Exception {
String id = "5630e9f55cd94e8b878c6f4d672191ad";
super.testGet(REST + "get/" + id);
}


/**
* 条件查询应用
*
* @throws Exception
*/
@Test
public void fetchAppList() throws Exception {
super.testPost(REST + "list", new BpTodo() {{
setTitle("测试待办事项");
setSysId("系统ID");
setTodoType("待办事项类型");
setSysUrl("系统URL");
setCreatorId("createId");
setUpdateId("updateId");
}});
}

/**
* 条件查询应用-分页
*
* @throws Exception
*/
@Test
public void fetchAppListPage() throws Exception {
super.testPost(REST + "list/page", new BpSysNotice() {{
setCurrentPage(1);
setPageSize(2);
}});
}
}

相关文章

评论系统未开启,无法评论!