zoukankan      html  css  js  c++  java
  • SpringBoot_07_Springboot test 使用mockito进行web测试

    一、前言

    使用mockito测试框架可以方便的进行web测试

    二、用法实例

    package com.ray.weixin.qy.controller;
    
    import com.ray.weixin.qy.ApplicationTests;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.Test;
    import org.springframework.http.MediaType;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    /**
     * @author : shira
     * @date : 2018/7/8
     * @time : 15:03
     * @desc :
     **/
    @Slf4j
    public class UserControllerTest  extends ApplicationTests {
    
    
    
    
        /**
         * 1.新增用户信息
         * @throws Exception
         */
        @Test
        public void testCreate() throws Exception {
            String content = "{
    " +
                    ""userid":"sunwukong",
    " +
                    ""name":"孙悟空",
    " +
                    ""department":[2],
    " +
                    ""position":"总经理",
    " +
                    ""mobile":"17636763734",
    " +
                    ""gender":"0",
    " +
                    ""email":"17636763734@qq.com"
    " +
                    "
    " +
                    "
    " +
                    "}";
    
            String result = mockMvc.perform(
                    post("/user")
                            .content(content)
                            .contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$.status").value(0))
                    .andReturn().getResponse().getContentAsString();
    
            log.info(result);
        }
    
    
        /**
         * 2.删除用户信息
         * @throws Exception
         */
        @Test
        public void testDelete() throws Exception {
    
            String userId="sunwukong";
    
            String result = mockMvc.perform(
                    delete("/user")
                            .param("userId", userId)
                            .contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$.status").value(0))
                    .andReturn().getResponse().getContentAsString();
    
            log.info(result);
        }
    
    
        /**
         * 3.修改用户信息
         * @throws Exception
         */
        @Test
        public void testUpdate() throws Exception {
            String content = "{
    " +
                    ""userid":"sunwukong",
    " +
                    ""name":"孙悟空",
    " +
                    ""department":[2],
    " +
                    ""position":"总经理",
    " +
                    ""mobile":"17636763734",
    " +
                    ""gender":"0",
    " +
                    ""email":"17636763734@qq.com"
    " +
                    "
    " +
                    "
    " +
                    "}";
            String result = mockMvc.perform(
                    put("/user")
                            .content(content)
                            .contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andExpect(status().isOk())
                    .andExpect(jsonPath("$.status").value(0))
                    .andReturn().getResponse().getContentAsString();
    
            log.info(result);
        }
    
    
        /**
         * 4.获取用户信息
         * @throws Exception
         */
        @Test
        public void testGet() throws Exception {
    
            String userId="sunwukong";
    
            String result = mockMvc.perform(
                    get("/user")
                            .param("userid", userId)
                            .contentType(MediaType.APPLICATION_JSON_UTF8))
                    .andExpect(status().isOk())
                    //.andExpect(jsonPath("$.length()").value(3))
                    .andExpect(jsonPath("$.status").value(0))
                    .andReturn().getResponse().getContentAsString();
    
            log.info(result);
        }
    
    }
    View Code

    三、用法详解

    四、参考资料

    1.SpringBoot与JUnit+Mockito 单元测试

  • 相关阅读:
    近段时间学习html和CSS的一些细碎总结
    循环队列
    【IOS】IOS高速入门之OC语法
    2014华为机试-字符串替换
    自己动手写操作系统--个人实践
    HBase学习(十四)LINUX下用Eclipse构建HBase开发环境
    IOS成长之路-Nsstring中搜索方法rangeOfString
    Java JDBC连接SQL Server2005错误:通过port 1433 连接到主机 localhost 的 TCP/IP 连接失败
    java 不同意同一账户不同IP 同一时候登录系统解决的方法 兼容IE Firefox
    十二.200多万元得到的创业教训--app名字是关键
  • 原文地址:https://www.cnblogs.com/shirui/p/9286042.html
Copyright © 2011-2022 走看看