在使用SpringBoot MockMvc Junit4进行单元测试之前使用SpringBoot MockMvc测试是一种快速的测试方法,必须在pom.xml文件中包含依赖关系
1
2
3
4
从属关系
groupid org.spring framework.boot/groupid
artifactidspring-boot-starter-test/artifact id
/dependency向测试类中添加以下注释
1
2
@runwith(springrunner.class ) ) )。
@SpringBootTest
控制器测试第1种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
package com.VI royal.user.controller;
import net.minidev.JSON.JSON object;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
importorg.spring framework.beans.factory.annotation.auto wired;
importorg.spring framework.boot.test.context.springboottest;
importorg.spring帧work.http.media type;
importorg.spring framework.test.context.JUnit4. spring runner;
importorg.spring框架. test.web.servlet.mock MVC;
importorg.spring框架. test.web.servlet.MVC result;
importorg.spring帧. test.web.servlet.result actions;
importorg.spring framework.test.web.servlet.request.mockhttpservletrequestbuilder;
importorg.spring framework.test.web.servlet.request.mockmvcrequestbuilders;
importorg.spring framework.test.web.servlet.result.mockmvcresulthandlers;
importorg.spring framework.test.web.servlet.result.mockmvcresultmatchers;
importorg.spring framework.test.web.servlet.setup.mockmvcbuilders;
importorg.spring framework.web.context.webapplicationcontext;
import java.util.HashMap;
import java.util.Map;
@runwith(springrunner.class ) ) )。
@SpringBootTest
publicclasssysusercontrollertest {
私密掩模MVC MVC;
@Autowired
隐私webapplicationcontextwac;
@Before
公共void设置() }
this.MVC=mockmvcbuilders.webappcontextsetup (this.WAC ).build );
}
@After
公共语音下载
}
@Test
public void add() throws Exception {
Map map = new HashMap();
map.put("username","tough");
map.put("password","123456");
map.put("phone","17623677587");
String jsonObject = JSONObject.toJSONString(map);
System.out.println(jsonObject);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/v1/sys/user/add")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonObject);
.andDo(MockMvcResultHandlers.print())
.andReturn();// 返回执行请求的结果
System.out.println(mvcResult.getResponse().getContentAsString());
}
}
第二种 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
package com.viroyal.user.controller;
import net.minidev.json.JSONObject;
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.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SysUserControllerTest {
private MockMvc mvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@After
public void tearDown() {
}
@Test
public void login() throws Exception {
Map map = new HashMap();
map.put("username","tough");
map.put("password","123456");
mvc.perform(MockMvcRequestBuilders.post("/v1/sys/user/login")
.contentType(MediaType.APPLICATION_JSON)
.content(JSONObject.toJSONString(map)))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("errorCode").value(1000));
}
Service层测试
原理和以上很相同,只需将dao层注入即可
12
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
package com.viroyal.smart.smokesensor.service;
import com.viroyal.smart.smokesensor.dao.SmokeSensorRptDao;
import com.viroyal.smart.smokesensor.domain.SmokeSensorRptPO;
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.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SmokeSensorDeviceServiceImplTest {
@Autowired
private SmokeSensorRptDao smokeSensorRptDao;
@Test
public void reportAll() throws Exception {
List list = new ArrayList();
String[] str = new String[]{"866971030530914","866971030530708"};
SmokeSensorRptPO smokeSensorRptPO = smokeSensorRptDao.findFirstByImeiInOrderByCtTimeDesc(Arrays.asList(str));
System.out.println(smokeSensorRptPO.toString());
}
}