MockMvc测试

SpringBoot MockMvc Junit4进行单元测试

前言

使用SpringBoot MockMvc测试是快速测试的一种手段,需要在pom.xml文件中加入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

在测试类中加入如下注解

1
2
@RunWith(SpringRunner.class)
@SpringBootTest

controller测试

第一种

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.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 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);

ResultActions result = mvc.perform(requestBuilder);

MvcResult mvcResult = result.andExpect(MockMvcResultMatchers.status().isOk())
.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层注入即可

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
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());
}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值