springboot利⽤mock进⾏junit单元测试,测试controller 1  spring-boot-starter-test内置mockito,添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2 ⽰例controller
package ller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @version v1.0spring boot面试题笔试题
* @Description: junit和mock单元测试⽰例
* @Author: shangcg
* @Date: 2019/12/24
*/
@RestController
public class UnitDemoController {
@RequestMapping(value = "/hello.json", method = RequestMethod.GET)
public String getListTag(HttpServletRequest request,
@RequestParam(value = "name", required = false, defaultValue = "0") String name) {
try {
return"hello :" + name;
} catch (Exception e) {
e.printStackTrace();
}
return"hello everyone !";
}
@RequestMapping(value = "/save.json", method = RequestMethod.POST)
public String saveTag(HttpServletRequest request,
@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "level", required = true) Integer level) {
try {
return"recive your param " + "name: " + name + " level: " + level;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3 ⽰例测试类
package ller;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.st.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.st.context.SpringBootTest;
import org.springframework.http.MediaType;
import st.context.junit4.SpringRunner;
import st.web.servlet.MockMvc;
import st.web.servlet.MvcResult;
import st.quest.MockMvcRequestBuilders;
import st.sult.MockMvcResultHandlers;
import st.sult.MockMvcResultMatchers;
import st.web.servlet.setup.MockMvcBuilders;
import org.t.WebApplicationContext;
/**
* @version v1.0
* @Description: TODO
* @Author: shangcg
* @Date: 2019/12/24
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UnitDemoControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();//建议使⽤这种
}
@Test  //对get⽅法的测试
public void testGetListTag() throws Exception {
MvcResult mvcResult = mockMvc.perform(
<("/hello.json")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.param("name", "shangcg")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
String content = Response().getContentAsString();
Assert.assertEquals("hello :shangcg", content);
}
@Test //对post测试
public void saveTag() throws Exception {
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.post("/save.json")
.contentType(MediaType.APPLICATION_JSON)
.param("name", "shangcg")
.param("level", "1")
).andExpect(MockMvcResultMatchers.status().isOk())
.
andDo(MockMvcResultHandlers.print())
.andReturn();
String content = Response().getContentAsString();
Assert.assertEquals("recive your param name: shangcg level: 1", content);    }
}
4 返回结果
5 因⽰例项⽬代码较多没法上传,需要源码请留⾔

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。