unitTestBase類武契,其他test類集成Base類
import com.alibaba.fastjson.JSONObject;
import com.isstech.cay.model.result.ResultCode;
import com.isstech.cay.utils.AssertUtil;
import org.junit.After;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnalysisApplicationTests {
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before
public void init() {
System.out.println("開始測試-----------------");
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc對象
}
@Test
public void test() {
}
@After
public void after() {
System.out.println("測試結(jié)束-----------------");
}
/**
* 添加
*
* @param url
* @param entity
* @throws Exception
*/
public JSONObject testAdd(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "添加失敗!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}
/**
* 修改
*
* @param url
* @param entity
* @throws Exception
*/
public JSONObject testUpdate(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "修改失斖臁!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}
/**
* 刪除
*
* @param url 刪除地址
* @throws Exception
*/
public JSONObject testDel(String url) throws Exception {
String responseString = mockMvc.perform(delete(url)
.contentType(MediaType.APPLICATION_JSON)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "刪除失斏静颉!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}
/**
* 查詢
*
* @param url
* @return
* @throws Exception
*/
public JSONObject testGet(String url) throws Exception {
String responseString = mockMvc.perform(get(url)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "GET查詢失斠唷!");
return jsonObject;
}
/**
* 查詢
*
* @param url
* @return
* @throws Exception
*/
public JSONObject testPost(String url, Object entity) throws Exception {
String requestJson = JSONObject.toJSONString(entity);
String responseString = mockMvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON).content(requestJson)).andDo(print())
.andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
JSONObject jsonObject = JSONObject.parseObject(responseString);
AssertUtil.isTrue(ResultCode.SUCCESS.code().equals(jsonObject.getInteger("status")), "POST查詢失斪竞痢氢橙!");
System.out.println(jsonObject.toJSONString());
return jsonObject;
}
}
api接口測試
import com.isstech.cay.model.BpSysNotice;
import com.isstech.cay.model.BpTodo;
import org.junit.Test;
/**
* 待辦事項(xiàng)管理
*
* @author Colin.Ye
* @version 1.0
* @ClassName BpSysTodoTests
* @date 2020/4/13
**/
public class BpSysTodoTests extends AnalysisApplicationTests {
private static final String REST = "/bp/todo/";
/**
* 新增通知
*/
@Test
@Transactional
@Rollback
public void add() throws Exception {
super.testAdd(REST + "add", new BpTodo() {{
// setId("5630e9f55cd94e8b878c6f4d672191ad");
setTitle("測試待辦事項(xiàng)");
setSysId("系統(tǒng)ID");
setTodoType("待辦事項(xiàng)類型");
setSysUrl("系統(tǒng)URL");
setCreatorId("createId");
setUpdateId("updateId");
}});
}
/**
* 修改通知
*/
@Test
@Transactional
@Rollback
public void update() throws Exception {
super.testUpdate(REST + "update", new BpTodo() {{
setId("5630e9f55cd94e8b878c6f4d672191ad");
setTitle("測試待辦事項(xiàng)1");
setSysId("系統(tǒng)ID1");
setTodoType("待辦事項(xiàng)類型1");
setSysUrl("系統(tǒng)URL1");
setCreatorId("createId1");
setUpdateId("updateId1");
}});
}
/**
* 刪除通知
*
* @throws Exception
*/
@Test
@Transactional
@Rollback
public void del() throws Exception {
String id = "5630e9f55cd94e8b878c6f4d672191ad";
super.testDel(REST + "delete/" + id);
}
/**
* 根據(jù)ID查詢通知信息
*
* @throws Exception
*/
@Test
public void get() throws Exception {
String id = "5630e9f55cd94e8b878c6f4d672191ad";
super.testGet(REST + "get/" + id);
}
/**
* 條件查詢應(yīng)用
*
* @throws Exception
*/
@Test
public void fetchAppList() throws Exception {
super.testPost(REST + "list", new BpTodo() {{
setTitle("測試待辦事項(xiàng)");
setSysId("系統(tǒng)ID");
setTodoType("待辦事項(xiàng)類型");
setSysUrl("系統(tǒng)URL");
setCreatorId("createId");
setUpdateId("updateId");
}});
}
/**
* 條件查詢應(yīng)用-分頁
*
* @throws Exception
*/
@Test
public void fetchAppListPage() throws Exception {
super.testPost(REST + "list/page", new BpSysNotice() {{
setCurrentPage(1);
setPageSize(2);
}});
}
}