Mock接口controller单元测试记录:
一:创建基础BaseJunit
/**
* 基础测试类
*
* @author xx
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = xxxx.class)
@WebAppConfiguration
@Transactional //打开的话测试之后数据可自动回滚
public class BaseJunit {
@Autowired
WebApplicationContext webApplicationContext;
protected MockMvc mvc;
protected String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjE2NzM5ODQ1fQ.qu0dsJ336XLm43p_QsuyZwbuAo3pYC1om3ZT7WeezfY";
@Before
public void setupMockMvc(){
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Before
public void initDatabase(){
}
@BeforeEach
void setUp() throws Exception {
if(!StringUtils.isEmpty(token)){
return;
}
String url = "/users/login";
String content = "{\"userName\": \"admin\", \"passWord\": \"\"}";
RequestBuilder request = post(url)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(content);
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
assertEquals(ResCode.SUCCESS.getCode(),jsonObject.getString("status_code"));
token = jsonObject.getJSONObject("payload").getString("token");
TestUtil.tokens=token;
}
@Test
public void Release() throws Exception {
}
二:创建带有请求头的Utils
public class TestUtil {
public static String tokens="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwiZXhwIjoxNjE2NzM5ODQ1f";
/**
* post请求
* @param url
* @param content body体内容
* @param mvc
* @param token token
* @return
* @throws Exception
*/
public static JSONObject postHttp(String url, String content, MockMvc mvc,String token) throws Exception {
RequestBuilder request = post(url)
.header("Authorization","token "+token)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(content);
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
return jsonObject;
}
/**
* get请求
* @param url
* @param mvc
* @param token token
* @return
* @throws Exception
*/
public static JSONObject getHttp(String url, MockMvc mvc,String token) throws Exception {
RequestBuilder request = get(url)
.header("Authorization","token "+token);
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
return jsonObject;
}
/**
* put请求
* @param url
* @param params 请求参数
* @param content body体内容
* @param mvc
* @param token
* @return
* @throws Exception
*/
public static JSONObject putHttp(String url, MultiValueMap<String, String> params, String content, MockMvc mvc,String token) throws Exception {
RequestBuilder request = put(url)
.header("Authorization","token "+token)
//.param("ownerId","1")
.params(params)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(content);
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
return jsonObject;
}
/**
* delete
* @param url
* @param content body体内容
* @param mvc
* @param token token
* @return
* @throws Exception
*/
public static JSONObject deleteHttp(String url, String content, MockMvc mvc,String token) throws Exception {
RequestBuilder request = delete(url)
.header("Authorization","token "+token)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(content);
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
JSONObject jsonObject = JSONObject.parseObject(result.getResponse().getContentAsString());
return jsonObject;
}
}
三:创建controller单元测试类
public class BaseLineControllerTest extends BaseJunit {
private String prefix="/baseline";
/**
* Method: list(@RequestParam(required = false) String projectId, @RequestParam(required = false) String domainCode, @RequestParam(required = false) String serviceName, @RequestParam(required = false, defaultValue = "10") Long pageSize, @RequestParam(required = false, defaultValue = "1") Long pageNum)
*/
@Test
public void List() throws Exception {
String url = String.format("%s%s",prefix,"?domainCode=&serviceName=&projectId=&pageNum=1&pageSize=10");
//String content = "{\"id\": \"1\"}";
JSONObject jsonObject = TestUtil.getHttp(url,mvc,token);
assertEquals(ResCode.SUCCESS.getCode(),jsonObject.getString("status_code"));
System.out.println("list");
}