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 单元测试

目录
相关文章
|
2月前
|
SQL 安全 测试技术
Web应用程序安全测试
Web应用程序安全测试
|
19天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
21 0
|
5天前
|
XML Web App开发 测试技术
python的Web自动化测试
【4月更文挑战第16天】Python在Web自动化测试中广泛应用,借助Selenium(支持多浏览器交互)、BeautifulSoup(解析HTML/XML)、Requests(发送HTTP请求)和Unittest(测试框架)等工具。测试步骤包括环境搭建、编写测试用例、初始化浏览器、访问页面、操作元素、验证结果、关闭浏览器及运行报告。注意浏览器兼容性、动态内容处理和错误处理。这些组合能提升测试效率和质量。
11 6
|
9天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
16 0
|
1月前
javaWeb服务详解(含源代码,测试通过,注释) ——web.xml
javaWeb服务详解(含源代码,测试通过,注释) ——web.xml
7 0
|
1月前
|
安全 测试技术 API
请描述在 Python WEB 开发中常用的测试方法。
请描述在 Python WEB 开发中常用的测试方法。
18 0
|
1月前
|
Web App开发 前端开发 测试技术
Web应用程序测试工具Selenium用法详解
Web应用程序测试工具Selenium用法详解
37 0
|
1月前
|
存储 搜索推荐 Java
springboot280基于WEB的旅游推荐系统设计与实现
springboot280基于WEB的旅游推荐系统设计与实现
|
1月前
|
Java 测试技术 数据库
springboot大学生体质测试管理系统
springboot大学生体质测试管理系统
|
1月前
|
搜索推荐 Java 数据库
springboot基于Web的社区医院管理服务系统
springboot基于Web的社区医院管理服务系统