get请求
post请求
文件的请求
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.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @decription:
* @author: zhuangxr002
* @date 2021/3/2 10:41
*/
@RunWith(SpringJUnit4ClassRunner.class) // SpringJUnit支持,由此引入Spring-Test框架支持!
@SpringBootTest(classes = Application.class)
@Transactional //单元测试自动回滚
public class NewsEndpointTest {
// 注入Spring 工厂
@Autowired
private WebApplicationContext wac;
// 伪造mvc环境
private MockMvc mockMvc;
private static final String FILE_REQUEST = "/api/file/";
@Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
//get请求
@Test
public void newsDetail() throws Exception {
// 参数
String result =
mockMvc.perform(get(REQUEST+"newsDetail")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.param("seq", "8"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(); //对返回字符串的json内容进行判断
assertEquals(0, CommonUtil.getReslutCode(result));
}
//post请求
@Test
public void list() throws Exception {
String content = "{\"title\":\"\",\"sourceName\":\"\",\"newsSummary\":\"\",\"bgnDt\":null,\"endDt\":null,\"page\":\"1\",\"limit\":\"5\"}";
//模拟发送请求
String result =
mockMvc.perform(post(REQUEST + "list")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(); //对返回字符串的json内容进行判断
assertEquals(0, CommonUtil.getReslutCode(result));
}
//file请求
@Test
public void uploadFile() throws Exception{
// 参数
MockMultipartFile firstFile = new MockMultipartFile("fileupload", "filename.txt", "text/plain", "some xml".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("fileupload", "other-file-name.data", "text/plain", "some other type".getBytes());
String result = mockMvc.perform(MockMvcRequestBuilders.multipart(FILE_REQUEST + "uploadEnclosure")
.file(firstFile)
.file(secondFile)
.param("business", "news")
.param("entityId","54325235235432543254325432"))
.andExpect(status().is(200))
.andReturn().getResponse().getContentAsString();
assertNotNull(result);
}
}
上面是单元测试案例。 MockMultipartFile的第一个是请求接口的参数名,跟api的接口参数对应fileupload
控制器的接口为
/**
* 共用上传附件等--信息
*/
@PostMapping("/uploadEnclosure")
@ResponseBody
public R uploadEnclosure(@RequestParam(value = "fileupload", required = false) MultipartFile[] fileupload,
HttpServletRequest request) {
boolean headImgFlag = false;
String businessType = request.getParameter("business");
String entityId = request.getParameter(ENTITY_ID_VARIABLE);
IamUserInfo userInfo = SecurityContext.getUser().getIamDetail();
String uid = userInfo.getUid();
List<Map<String, Object>> resMap ;
try {
resMap = newsService.uploadFile(uid, entityId, businessType, fileupload, headImgFlag);
}catch (DefException e ){
return R.error(e.getMsg());
}catch (Exception e1){
return R.error(e1.getMessage());
}
}