springboot利用mock进行junit单元测试,测试controller

时间:2023-03-09 18:10:17
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 com.shangcg.controller;

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.0
* @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 com.shangcg.controller;

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.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.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; /**
* @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(
MockMvcRequestBuilders.get("/hello.json")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8")
.param("name", "shangcg")
).andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn(); String content = mvcResult.getResponse().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 = mvcResult.getResponse().getContentAsString();
Assert.assertEquals("recive your param name: shangcg level: 1", content);
}
}

4 返回结果

springboot利用mock进行junit单元测试,测试controller

5 因示例项目代码较多没法上传,需要源码请留言