美文网首页
UnitTest单元测试

UnitTest单元测试

作者: 丿捺人生 | 来源:发表于2020-04-29 16:18 被阅读0次

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("测试结束-----------------");
    }

    /**
     * 添加
     *
     * @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;

/**
 * 待办事项管理
 *
 * @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("测试待办事项");
            setSysId("系统ID");
            setTodoType("待办事项类型");
            setSysUrl("系统URL");
            setCreatorId("createId");
            setUpdateId("updateId");
        }});
    }

    /**
     * 修改通知
     */
    @Test
    @Transactional
    @Rollback
    public void update() throws Exception {
        super.testUpdate(REST + "update", new BpTodo() {{
            setId("5630e9f55cd94e8b878c6f4d672191ad");
            setTitle("测试待办事项1");
            setSysId("系统ID1");
            setTodoType("待办事项类型1");
            setSysUrl("系统URL1");
            setCreatorId("createId1");
            setUpdateId("updateId1");
        }});
    }

    /**
     * 删除通知
     *
     * @throws Exception
     */
    @Test
    @Transactional
    @Rollback
    public void del() throws Exception {
        String id = "5630e9f55cd94e8b878c6f4d672191ad";
        super.testDel(REST + "delete/" + id);
    }

    /**
     * 根据ID查询通知信息
     *
     * @throws Exception
     */
    @Test
    public void get() throws Exception {
        String id = "5630e9f55cd94e8b878c6f4d672191ad";
        super.testGet(REST + "get/" + id);
    }


    /**
     * 条件查询应用
     *
     * @throws Exception
     */
    @Test
    public void fetchAppList() throws Exception {
        super.testPost(REST + "list", new BpTodo() {{
            setTitle("测试待办事项");
            setSysId("系统ID");
            setTodoType("待办事项类型");
            setSysUrl("系统URL");
            setCreatorId("createId");
            setUpdateId("updateId");
        }});
    }

    /**
     * 条件查询应用-分页
     *
     * @throws Exception
     */
    @Test
    public void fetchAppListPage() throws Exception {
        super.testPost(REST + "list/page", new BpSysNotice() {{
            setCurrentPage(1);
            setPageSize(2);
        }});
    }
}

相关文章

网友评论

      本文标题:UnitTest单元测试

      本文链接:https://www.haomeiwen.com/subject/wsknwhtx.html