前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot_07_Springboot test 使用mockito进行web测试

SpringBoot_07_Springboot test 使用mockito进行web测试

作者头像
shirayner
发布2018-08-10 10:04:09
8150
发布2018-08-10 10:04:09
举报
文章被收录于专栏:Java成神之路Java成神之路

一、前言

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

二、用法实例

代码语言:javascript
复制
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 = "{\n" +
                "\"userid\":\"sunwukong\",\n" +
                "\"name\":\"孙悟空\",\n" +
                "\"department\":[2],\n" +
                "\"position\":\"总经理\",\n" +
                "\"mobile\":\"17636763734\",\n" +
                "\"gender\":\"0\",\n" +
                "\"email\":\"17636763734@qq.com\"\n" +
                "\n" +
                "\n" +
                "}";

        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 = "{\n" +
                "\"userid\":\"sunwukong\",\n" +
                "\"name\":\"孙悟空\",\n" +
                "\"department\":[2],\n" +
                "\"position\":\"总经理\",\n" +
                "\"mobile\":\"17636763734\",\n" +
                "\"gender\":\"0\",\n" +
                "\"email\":\"17636763734@qq.com\"\n" +
                "\n" +
                "\n" +
                "}";
        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);
    }

}

三、用法详解

四、参考资料

1.SpringBoot与JUnit+Mockito 单元测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、用法实例
  • 三、用法详解
  • 四、参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档