package com.sandu.ximon.admin.base;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.junit.Assert;
|
import org.junit.Before;
|
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.web.context.WebApplicationContext;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
/**
|
* @author chenjiantian
|
* @date 2022/1/12 11:20
|
*/
|
@RunWith(SpringRunner.class)
|
@SpringBootTest
|
public class BaseTest {
|
@Autowired
|
private WebApplicationContext wac;
|
private MockMvc mockMvc;
|
|
@Before
|
public void setup() {
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
}
|
|
public MockMvc getMvc() {
|
return mockMvc;
|
}
|
|
|
public void checkPost(String url,Object param) throws Exception{
|
MockHttpServletRequestBuilder builder = BaseMockMvcRequestBuilders.post(url);
|
if(param != null){
|
builder.content((new ObjectMapper()).valueToTree(param).toString());//请求的参数
|
}
|
ResultActions resultActions = mockMvc.perform(builder);
|
JSONObject response = ResultActionsUtil.getResponse(resultActions);
|
//打印全部信息
|
resultActions.andDo(print());
|
//对数据进行预期的判断
|
Assert.assertEquals("20000", response.getString("code"));
|
}
|
|
public void checkGet(String url) throws Exception{
|
MockHttpServletRequestBuilder builder = BaseMockMvcRequestBuilders.get(url);
|
ResultActions resultActions = mockMvc.perform(builder);
|
JSONObject response = ResultActionsUtil.getResponse(resultActions);
|
//打印全部信息
|
resultActions.andDo(print());
|
//对数据进行预期的判断
|
Assert.assertEquals("20000", response.getString("code"));
|
}
|
}
|