之前我在项目中进行单元测试的时候,是这种情况。首先添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
即可进行测试
现在我们使用MockMvc测试,使用相同的依赖
package com.zzmj.managersystem.dictionarymanager;
import org.junit.jupiter.api.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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
/**
* @author Wangzt
* @version 1.0
* @date 2019/11/27 11:55
* @email wangzt2012s@126.com
* @description:
*/
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class DeviceModelDictControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void getDeviceModelList() throws Exception {
String expectedContent = "{\"code\":1000,\"msg\":\"查询数据成功!\",\"data\":{\"items\":[{\"id\":\"1\",\"devicemodel_id\":\"1\",\"devicemodel_name\":\"1\",\"devicemodel_alias\":\"1\",\"devicemodel_code\":\"1\",\"devicemodel_desc\":\"1\",\"sortcode\":\"1\",\"system_id\":\"1\",\"system_ids\":\"1\",\"operator\":\"1\",\"updatetime\":\"1574770121268\"}],\"total\":4}}";
mockMvc.perform(MockMvcRequestBuilders.get("/ManagerSystem/DictionaryManager/DeviceModelDict/DeviceModel/1/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectedContent))
.andReturn();
}
@Test
public void getDeviceModelListBySystemId() throws Exception {
String expectedContent = "{\"code\":1000,\"msg\":\"查询数据成功!\",\"data\":{\"items\":[{\"id\":\"5f006b28-dd5f-4425-a262-55a394b44c52\",\"devicemodel_id\":\"41975f6c-6ca2-4df3-b493-f66fa9d55947\",\"devicemodel_name\":\"1\",\"devicemodel_alias\":\"1\",\"devicemodel_code\":\"1\",\"devicemodel_desc\":\"1\",\"sortcode\":\"1\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"operator\":null,\"updatetime\":\"1574838399356\"},{\"id\":\"80e2edc4-3522-45ca-af26-331e34515d55\",\"devicemodel_id\":\"75767497-6c56-451b-805d-b308d49e64c7\",\"devicemodel_name\":\"支架模型\",\"devicemodel_alias\":\"zj\",\"devicemodel_code\":\"11\",\"devicemodel_desc\":\"111\",\"sortcode\":\"2\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"operator\":null,\"updatetime\":\"1574838440682\"}],\"total\":3}}";
mockMvc.perform(MockMvcRequestBuilders.get("/ManagerSystem/DictionaryManager/DeviceModelDict/DeviceModel/BySystemId/cae590d5-52b4-4bda-9982-5fc82e6bb524/1/2")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectedContent))
.andReturn();
}
@Test
public void addDeviceModel() throws Exception {
String expectedContent = "{\"code\":1001,\"msg\":\"添加数据成功!\",\"data\":{\"id\":\"5ab09710-9228-495e-94e3-8141e6935f86\",\"devicemodel_id\":\"bd6dfc81-704c-4278-9a15-d9b2ae313184\",\"devicemodel_name\":\"液压模型\",\"devicemodel_alias\":\"yy\",\"devicemodel_code\":\"22\",\"devicemodel_desc\":\"22\",\"sortcode\":\"3\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"operator\":null,\"updatetime\":\"1574838470249\"}}";
String content = "{\"devicemodel_name\":\"液压模型\",\"devicemodel_alias\":\"yy\",\"devicemodel_code\":\"22\",\"devicemodel_desc\":\"22\",\"sortcode\":\"3\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",}";
mockMvc.perform(MockMvcRequestBuilders.post("/ManagerSystem/DictionaryManager/DeviceModelDict/DeviceModel")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectedContent))
.andReturn();
}
@Test
public void editDeviceModel() throws Exception {
String expectedContent = "{\"code\":1002,\"msg\":\"编辑数据成功!\",\"data\":{\"id\":null,\"devicemodel_id\":\"bd6dfc81-704c-4278-9a15-d9b2ae313184\",\"devicemodel_name\":\"液压模型111\",\"devicemodel_alias\":\"yy\",\"devicemodel_code\":\"22\",\"devicemodel_desc\":\"22\",\"sortcode\":\"3\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"operator\":null,\"updatetime\":\"1574838644995\"}}";
String content = "{\"devicemodel_id\":\"bd6dfc81-704c-4278-9a15-d9b2ae313184\",\"devicemodel_name\":\"液压模型111\",\"devicemodel_alias\":\"yy\",\"devicemodel_code\":\"22\",\"devicemodel_desc\":\"22\",\"sortcode\":\"3\",\"system_id\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",\"system_ids\":\"cae590d5-52b4-4bda-9982-5fc82e6bb524\",}";
mockMvc.perform(MockMvcRequestBuilders.post("/ManagerSystem/DictionaryManager/DeviceModelDict/DeviceModel")
.contentType(MediaType.APPLICATION_JSON)
.content(content)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectedContent))
.andReturn();
}
@Test
public void deleteSystem() throws Exception {//(批量)删除
String expectedContent = "{\"code\":1003,\"msg\":\"删除数据成功!\"}";
mockMvc.perform(MockMvcRequestBuilders.delete("/ManagerSystem/DictionaryManager/DeviceModelDict/DeviceModel/2c844196-9d8b-4680-96c1-c3ae3438fe5f")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectedContent))
.andReturn();
}
}
@RunWith(SpringRunner.class),就是指用SpringRunner来运行,其中
SpringJUnit4ClassRunner 和 SpringRunner 区别是什么?
在官方文档中有如下这句话:
SpringRunner is an alias for the SpringJUnit4ClassRunner
@SpringBootTest是SpringBoot的一个用于测试的注解,通过SpringApplication在测试中创建ApplicationContext。
@AutoConfigureMockMvc是用于自动配置MockMvc
上图我使用的restful风格的接口形式,另外数据我使用的json形式。大家可以百度json将数据自动转json格式进行测试。
另外我看到了网上有这种测试方式
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockXXXTest {
@Autowired
protected MockMvc mockMvc;
@MockBean
private XXXDao xxxtDao;
@Test
public void TestXXX() throws Exception {
List<Map<String,Object>> dataList= new ArrayList<>();
Map m=new HashMap();
m.put("name","test");
dataList.add(m);
Mockito.when(xxxDao.findMapBySql(Mockito.anyString(),Mockito.anyList())).thenReturn(dataList);
Mockito.when(xxxDao.findTotalCountBySql(Mockito.anyString(),Mockito.any())).thenReturn((long)1);
MvcResult result = mockMvc.perform(
MockMvcRequestBuilders.get("/xxxController/xxx_query")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.param("xxx","xxx")
)
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
System.out.println(result);
}
}
他这个应该是没有使用restful风格的接口,另外他将参数放在了 param()方法里面
这辈子坚持与不坚持都不可怕,怕的是独自走在前进的道路上!!!
欢迎加入技术群聊