使用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是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用.码云地址 ...
随机推荐
- 用来遍历map集合的方法
map集合是以键值对进行存储值的,所以遍历map集合无非就是获取键和值,根据实际需求,进行获取键和值. 1.无非就是通过map.keySet()获取到值,然后根据键获取到值. for(String s ...
- 在cmd运行窗口运行.py文件
步骤
- Jfinal undertow本地运行加载静态文件
undertow部署文档中可配置静态资源也可以添加磁盘目录作为项目中得静态文件访问 undertow.resourcePath = src/main/webapp, D:/static 这样配置就可以 ...
- 开发第一个maven示例
mavenDemo目录如下: 在webapp下新建index.jsp文件 浏览器访问:http://localhost:8080/mavenDemo/index.jsp
- 从设计稿到实现React应用(分类数据处理)
1. 确定设计稿和数据 设计稿: 数据: [ {category: "Sporting Goods", price: "$49.99", stocked: tr ...
- 遍历器Iterator--指针对象
一. 什么是遍历器 1. 遍历器对象(Iterator) 遍历器对象本质上是一个指针对象,该对象有一个next方法,调用next方法返回一个 含有value和done属性的对象{value: val/ ...
- 超过20g的文件+上传
demo下载地址:jsp-Eclipse,jsp-MyEclipse,PHP,ASP.NET 教程:ASP.NET,JSP,PHP 一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件 ...
- surprise库官方文档分析(一)
一:入门 1.基本用法 (1).自动交叉验证 Surprise有一套内置的 算法和数据集供您使用.在最简单的形式中,只需几行代码即可运行交叉验证程序: from surprise import SVD ...
- CSPS模拟86-87
模拟86 T1,烧水,按位统计贡献,利用某种sao操作避免数位dp #include<iostream> #include<cstdio> #include<cstrin ...
- PHP 多维数组 根据字段排序
这个太常用了,记录下.方便后面用 /** * @param $arr [要排序的数组] * @param $condition [要排序的条件, for array('id'=>SORT_DES ...