1 package com.yunen.camera.test;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.context.SpringBootTest;
8 import org.springframework.http.MediaType;
9 import org.springframework.mock.web.MockHttpSession;
10 import org.springframework.test.annotation.Rollback;
11 import org.springframework.test.context.junit4.SpringRunner;
12 import org.springframework.test.web.servlet.MockMvc;
13 import org.springframework.test.web.servlet.MvcResult;
14 import org.springframework.test.web.servlet.ResultActions;
15 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
16 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
17 import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
18 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
19 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
20 import org.springframework.web.context.WebApplicationContext;
21
22 import com.YunenCameraManagerApplication;
23 import com.alibaba.fastjson.JSONObject;
24 import com.alibaba.fastjson.serializer.SerializerFeature;
25
26 import lombok.extern.slf4j.Slf4j;
27
28 /**
29 * 相机预置位测试类
30 *
31 * @version V1.0
32 * @author songxiaotong
33 * @date 2018年9月17日 下午2:08:14
34 * @Description
35 */
36 @Slf4j
37 @Rollback(false)
38 // 让 JUnit 运行 Spring 的测试环境, 获得 Spring 环境的上下文的支持
39 @RunWith(SpringRunner.class)
40 // 获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
41 @SpringBootTest(classes = YunenCameraManagerApplication.class)
42 public class CameraPreSetControllerTest {
43 /**
44 * WebApplicationContext
45 */
46 @Autowired
47 private WebApplicationContext wac;
48
49 private MockMvc mockMvc;
50
51 /**
52 * 初始化mvc请求对象
53 *
54 * @throws Exception
55 */
56 @Before
57 public void setUp() throws Exception {
58 this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
59 }
60
61 /**
62 * 新增
63 *
64 * @author songxiaotong
65 */
66 @Test
67 public void create() {
68 try {
69 MockHttpSession session = new MockHttpSession();
70
71 JSONObject param = new JSONObject();
72 param.put("cameraId", "110");
73 param.put("name", "预置位名称");
74 param.put("zoom", 1222.1);
75 param.put("pan", 2222.2);
76 param.put("tilt", 3222.3);
77
78 MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
79 .post("/yunen/camera_pre_set/create");
80 requestBuilder.contentType(MediaType.APPLICATION_JSON);
81
82 requestBuilder.content(JSONObject.toJSONString(param,
83 SerializerFeature.WriteMapNullValue));
84 // requestBuilder.content(param.toString());
85
86 requestBuilder.session(session);
87
88 // 执行一个请求;
89 ResultActions result = mockMvc.perform(requestBuilder);
90
91 // 添加执行完成后的断言
92 result.andExpect(MockMvcResultMatchers.status().isOk());
93
94 // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
95 result.andDo(MockMvcResultHandlers.print());
96
97 // 表示执行完成后返回相应的结果。
98 MvcResult mvcResult = result.andReturn();
99
100 log.info(mvcResult.getResponse().getContentAsString());
101 } catch (Exception e) {
102 log.error(e.getMessage());
103 }
104 }
105
106 /**
107 * 更新
108 *
109 * @author songxiaotong
110 */
111 @Test
112 public void update() {
113 String url = "/yunen/camera_pre_set/update";
114 JSONObject param = new JSONObject();
115 param.put("id", 1);
116 param.put("cameraId", "110");
117 param.put("name", "更新后的预置位名称");
118 param.put("zoom", 1222.1);
119 param.put("pan", 2222.2);
120 param.put("tilt", 3222.3);
121 // 调用测试模拟方法
122 requestMock(url, param);
123 }
124
125 /**
126 * http请求模拟
127 *
128 * @param url 请求地址
129 * @param param 请求参数
130 * @author songxiaotong
131 */
132 private void requestMock(String url, JSONObject param) {
133 try {
134 MockHttpSession session = new MockHttpSession();
135
136 MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
137 .post(url);
138 requestBuilder.contentType(MediaType.APPLICATION_JSON);
139
140 requestBuilder.content(JSONObject.toJSONString(param,
141 SerializerFeature.WriteMapNullValue));
142 // requestBuilder.content(param.toString());
143
144 requestBuilder.session(session);
145
146 // 执行一个请求;
147 ResultActions result = mockMvc.perform(requestBuilder);
148
149 // 添加执行完成后的断言
150 result.andExpect(MockMvcResultMatchers.status().isOk());
151
152 // 添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
153 result.andDo(MockMvcResultHandlers.print());
154
155 // 表示执行完成后返回相应的结果。
156 MvcResult mvcResult = result.andReturn();
157
158 log.info(mvcResult.getResponse().getContentAsString());
159 } catch (Exception e) {
160 log.error(e.getMessage());
161 }
162 }
163 }