package com.ficus.face.product.spring.tutorial.score.controller;
import com.mongodb.util.JSON;
import net.minidev.json.JSONObject;
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.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class CourseControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() throws Exception{
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //构建MockMVC
}
public MockHttpServletRequestBuilder getBuilder(String method, String url) throws Exception {
MockHttpServletRequestBuilder builder= null;
switch(method){
case "post": builder =MockMvcRequestBuilders.post(url); break;
case "delete": builder =MockMvcRequestBuilders.delete(url); break;
case "put" : builder = MockMvcRequestBuilders.put(url); break;
default: throw new Exception("非法请求方式");
}
return builder;
}
public Map request(String method, String url, Map map ) throws Exception {
MockHttpServletRequestBuilder builder = getBuilder(method,url);
MvcResult mvcResult = mockMvc.perform(builder
.contentType(MediaType.APPLICATION_JSON)
.content(JSONObject.toJSONString(map))
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
String result = mvcResult.getResponse().getContentAsString();
Map maps = (Map)JSON.parse(result);
return maps;
}
@Test
public void queryScoreByStudentTest() throws Exception{
Map<String, Object> map = new HashMap<>();
map.put("id", "112");
// MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/education/yitu/score_center/api/v1/course/query_score_by_course")
// .contentType(MediaType.APPLICATION_JSON)
// .content(JSONObject.toJSONString(map))
// .accept(MediaType.APPLICATION_JSON))
// .andExpect(MockMvcResultMatchers.status().isOk())
// .andDo(MockMvcResultHandlers.print())
// .andReturn();
// String result = mvcResult.getResponse().getContentAsString();
// Map maps = (Map)JSON.parse(result);
Map maps = request("post",
"/education/yitu/score_center/api/v1/course/query_score_by_course",
map);
String rtn = (String)maps.get("rtn");
Assert.assertEquals("0",rtn);
}
}
来源:https://blog.csdn.net/qq_37325947/article/details/102757188