zoukankan      html  css  js  c++  java
  • ava Junit 单元测试controller层接口的方法

    首先要注入MockMvc(引入的包为:import org.springframework.test.web.servlet.MockMvc;)

    @Autowired
        private MockMvc mvc;

    1.get方法,参数类型为@RequestParam

    @Test
        public void searchFiles() throws Exception {
            //文件搜索
            RequestBuilder request;
            request = get("/api/v1/folders").param("page", "1")
                    .param("pageSize", "50")
                    .param("parentId", "-1")
                    .param("order", "createTime")
                    .param("orderType", "desc");
            mvc.perform(request)
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        }

    2.get方法,参数类型为@PathVariable

    @Test
        public void getFoldersByParentId() throws Exception {
            //文件夹搜索
            RequestBuilder request;
            request = get("/api/v1/folders/{parentId}/folders", -1);
            mvc.perform(request)
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        }

    3.post方法,参数类型为@RequestBody

    @Test
        public void createNewOrEditFile() throws Exception {
            //创建文件夹
            RequestBuilder request;
            request = post("/api/v1/folders")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .content("{"name": "测试", "dir": 1, "parentId": -1}");
            mvc.perform(request)
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        }

    4.需要获得接口得返回值用以进行其他接口得关联

        @Test
        public void createNewOrEditFile() throws Exception {
            //创建文件夹
            RequestBuilder request;
            request = post("/api/v1/folders")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .content("{"name": "测试", "dir": 1, "parentId": -1}");
            String re = mvc.perform(request)
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andReturn().getResponse().getContentAsString();
        }

    5.delete方法

        @Test
        public void deleteFiles() throws Exception {
            RequestBuilder request;
            request = delete("/api/v1/folders")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .content("{"ids":  [430]}");
            mvc.perform(request)
                    .andExpect(status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        }

    ref:SpringBoot基础之MockMvc单元测试 - 云+社区 - 腾讯云 (tencent.com)

  • 相关阅读:
    MSSQL Extension For Visual Studio Code
    钉钉开发系列(三)API的调用
    DataTable转换为Model
    钉钉开发系列(十一)钉钉网页扫码登录
    EntityFramework获取数据库的时间
    EntityFramework连接串的调用时传入
    钉钉开发系列(十)SaltUI与WebApi交互
    钉钉开发系列(九)SaltUI在VS中的开发
    钉钉开发系列(八)二维码扫描登录的实现
    钉钉开发系列(七)媒体文件的上传与下载
  • 原文地址:https://www.cnblogs.com/cy0628/p/15428002.html
Copyright © 2011-2022 走看看