SpringBoot_07_Springboot test 使用mockito进行web测试

简介: 一、前言 使用mockito测试框架可以方便的进行web测试   二、用法实例 package com.ray.weixin.qy.controller; import com.ray.

一、前言

使用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 = "{\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);
    }

}
View Code

 

三、用法详解

 

 

 

 

 

四、参考资料

1.SpringBoot与JUnit+Mockito 单元测试

目录
相关文章
|
12天前
|
Java 测试技术 Android开发
SpringBoot如何写好单元测试
SpringBoot如何写好单元测试
|
13天前
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
23 0
|
18天前
|
Java Maven
SpringBoot项目的用maven插件打包报Test错误
SpringBoot项目的用maven插件打包报Test错误
|
3天前
|
JavaScript 前端开发 Java
基于SpringBoot+Vue+uniapp的在线开放课程的Web前端的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线开放课程的Web前端的详细设计和实现(源码+lw+部署文档+讲解等)
|
4天前
|
JavaScript Java 测试技术
基于springboot+vue.js的基于Web教师个人成果管理系统附带文章和源代码设计说明文档ppt
基于springboot+vue.js的基于Web教师个人成果管理系统附带文章和源代码设计说明文档ppt
15 7
|
4天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的在线测试管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线测试管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
5天前
|
数据可视化 数据挖掘 Java
springboot+vue体质测试数据分析及可视化设计(源码+文档)
体质测试数据分析及可视化设计实现了以下功能: 管理员:首页、个人中心、学生管理、教师管理、日常运动管理、运动分析管理、成绩信息管理、论坛管理、系统管理, 学生:首页、个人中心、日常运动管理、运动分析管理、成绩信息管理、论坛管理, 教师:首页、个人中心、日常运动管理、运动分析管理、成绩信息管理、系统管理, 前台首页:首页、论坛信息、公告信息、个人中心、后台管理、客服模块的修改维护操作。
|
19天前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
21 2
|
7天前
|
Web App开发 测试技术 API
自动化测试工具Selenium的深度解析
【5月更文挑战第27天】本文旨在深入剖析自动化测试工具Selenium,探讨其架构、原理及应用。通过对其核心组件、运行机制及在实际项目中的应用案例进行详细解读,以期为软件测试人员提供全面、深入的理解与实践指导。
|
2天前
|
运维 安全 网络架构
【计算巢】网络模拟工具:设计与测试网络架构的有效方法
【6月更文挑战第1天】成为网络世界的超级英雄,利用网络模拟工具解决复杂架构难题!此工具提供安全的虚拟环境,允许自由设计和测试网络拓扑,进行性能挑战和压力测试。简单示例代码展示了创建网络拓扑的便捷性,它是网络设计和故障排查的“魔法棒”。无论新手还是专家,都能借助它探索网络的无限可能,开启精彩冒险!快行动起来,你会发现网络世界前所未有的乐趣!
【计算巢】网络模拟工具:设计与测试网络架构的有效方法

热门文章

最新文章