使用MockMvc进行springboot调试(SpringbootTest)
测试前关闭web项目。springboot启动程序WebApplication.class
- 笔者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT
代码如下:
- import com.xxx.web.WebApplication;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.http.MediaType;
- import org.springframework.mock.web.MockHttpSession;
- import org.springframework.test.context.junit4.SpringRunner;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
- import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
- import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- import org.springframework.web.context.WebApplicationContext;
- import java.time.Instant;
- import java.util.Random;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = WebApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
- @AutoConfigureMockMvc
- public class TestAppController {
- @Autowired
- private WebApplicationContext context;
- @Autowired
- private MockMvc mvc;
- private MockHttpSession session;// 1.定义一个变量保存session
- String pathOnClasspath;
- @Before
- public void setUp() throws Exception {
- mvc = MockMvcBuilders.webAppContextSetup(context).build();
- session = new MockHttpSession(); //2.初始化
- }
- @Test
- public void login() throws Exception {
- // 登陆
- MockHttpServletRequestBuilder loginRequestBuilder = MockMvcRequestBuilders.post("/user2/login")
- .param("loginName", "test")
- .param("password", "567")
- .contentType(MediaType.APPLICATION_JSON_UTF8)
- .accept(MediaType.APPLICATION_JSON)
- .session(session);//3.当某个请求需要session时,直接在构造器中绑定需要的session
- mvc.perform(loginRequestBuilder).andDo(MockMvcResultHandlers.print());
- }
- @Test
- public void save() throws Exception {
- //先登录
- login();
- mvc.perform(post("/app/save")
- .param("name","测试")
- .param("categoryId","567")
- .param("description","休闲益智类游戏语音识别测试")
- .session(session)
- .contentType(MediaType.APPLICATION_JSON))
- .andExpect(status().isOk())
- //.andExpect(jsonPath("$",hasSize(1)))
- //.andExpect(jsonPath("$.message").value(is("保存成功")))
- //.andExpect(jsonPath("$.message"),is("保存成功"))
- .andDo(MockMvcResultHandlers.print());
- }
- @Test
- public void update() throws Exception{
- Random rnd = new Random();
- int id = rnd.nextInt(6);
- mvc.perform(
- post("/app/update")
- .param("id", String.valueOf(id))
- .param("name", String.format("测试%s", Instant.now().toEpochMilli()))
- .param("description", "描述12121")
- ).andDo(MockMvcResultHandlers.print());
- }
- @Test
- public void list() throws Exception {
- mvc.perform(get("/app/list")
- .contentType(MediaType.TEXT_HTML))
- .andExpect(status().isOk())
- .andDo(MockMvcResultHandlers.print());
- }
- @Test
- public void filteredList() throws Exception {
- mvc.perform(post("/app/list")
- .param("keyword","111")
- .contentType(MediaType.TEXT_HTML))
- .andExpect(status().isOk())
- .andDo(MockMvcResultHandlers.print());
- }
- @Test
- public void testisDuplicatedName() throws Exception
- {
- mvc.perform(post("/app/isDuplicatedName")
- .param("name","测试")
- ).andDo(MockMvcResultHandlers.print());
- }
- }
测试输出效果
其他参考:
https://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-write-clean-assertions-with-jsonpath/
使用MockMvc进行springboot调试(SpringbootTest)的更多相关文章
- SpringBoot基础之MockMvc单元测试
SpringBoot创建的Maven项目中,会默认添加spring-boot-starter-test依赖.在<5分钟快速上手SpringBoot>中编写的单元测试使用了MockMvc.本 ...
- SpringBoot配置文件(1)
配置文件 1.配置文件 SpringBoot使用一个全局的配置文件 application.properties application.yml 配置文件名是固定的: 他的作用是修改SpringBoo ...
- SpringBoot详解
1.Hello,World! 1.1.SpringBoot简介 回顾什么是Spring Spring是一个开源框架,2003 年兴起的一个轻量级的Java 开发框架,作者:Rod Johnson . ...
- SpringBoot Maven项目 Helloworld 测试
SpringBoot 化繁为简,简化配置 SpringBoot官方:http://projects.spring.io/spring-boot/SpringBoot使用介绍:http://blog.c ...
- [02-01]Java学习路线(完整详细版)
Java基础课程 Java基础课程内容涉及:Java开发介绍.Java数组.Java面向对象.常用基础类.集合.IO流.多线程.异常.网络.反射. 第一阶段:Java基础 1 第一部分:Java开发介 ...
- SpringBoot------MockMvc单元测试
1.添加测试依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...
- 单元测试 - SpringBoot2+Mockito实战
单元测试 - SpringBoot2+Mockito实战 在真实的开发中,我们通常是使用SpringBoot的,目前SpringBoot是v2.4.x的版本(SpringBoot 2.2.2.RELE ...
- SpringBoot------单元测试
1.添加测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- 分布式任务调度平台XXL-JOB学习笔记一
分布式任务调度平台XXL-JOB学习笔记一 XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用.码云地址 ...
随机推荐
- xcode 查看stastic
点GPU 双击柱状图 从上面list里点performance
- 数据库概念 MySQL语法
数据库概念 将保存的数据部分,存到一个公共的地方,所有的用户涉及到数据相关都必须来这个公共地方查找 MySQL 本质就是一款基于网络通信的应用软件,任何基于网络通信的软件底层都是socket 可以把M ...
- 我们什么时候应该在C程序中使用指针?
回答: 传递大型结构喜欢服务器请求或响应数据包. 实现链表和二叉树. 使用GPIO或硬件寄存器. 从函数中获取地址或更新值(通过引用调用) 创建动态数组. 使用函数指针创建回调函数. 注意:除此之外, ...
- 2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理
题意 求解 $n$ 个模方程 $x \equiv a (mod \ b)$,不保证模数互素($1 \leq n \leq 100$,$0 \leq b < a< 10^5$). 分析 套扩 ...
- HDU-1028-Ignatius and the Princess III(母函数)
链接: https://vjudge.net/problem/HDU-1028 题意: "Well, it seems the first problem is too easy. I wi ...
- Java处理小数点后几位
//方式一: //四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); , BigDecimal.ROUND_HALF_UP). ...
- Python3 报错'latin-1' codec can't encode character 解决方案
Python3 报错'latin-1' codec can't encode character 解决方案 在更新数据库操作时,报错: UnicodeEncodeError: 'latin-1' co ...
- [笔记] 三元环 && 四元环计数
Thanks to i207M && iki9! 三元环计数 无向图的三元环计数 我们首先需要对无向边按一定规则定向: 设 \(in[u]\) 表示 \(u\) 的度数 若 \(in[ ...
- E:nth-of-type(n)
E:nth-of-type(n) 语法: E:nth-of-type(n) { sRules } 说明: 匹配同类型中的第n个同级兄弟元素E.深圳dd马达 要使该属性生效,E元素必须是某个元素的子元素 ...
- MongoDB 分片管理(三)服务器管理
MongoDB 分片管理(三)服务器管理