没有合适的资源?快使用搜索试试~ 我知道了~
首页基于MockMvc进行springboot调试(SpringbootTest)
基于MockMvc进行springboot调试(SpringbootTest)
8 下载量 86 浏览量
更新于2023-03-16
评论 1
收藏 69KB PDF 举报
主要介绍了基于MockMvc进行springboot调试(SpringbootTest),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源详情
资源评论
资源推荐
基于基于MockMvc进行进行springboot调试调试(SpringbootTest)
主要介绍了基于MockMvc进行springboot调试(SpringbootTest),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了基于MockMvc进行springboot调试(SpringbootTest),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
价值,需要的朋友可以参考下
测试前关闭web项目。springboot启动程序WebApplication.class
笔者本地自定了端口SpringBootTest.WebEnvironment.DEFINED_PORT
代码如下:代码如下:
import com.netmarch.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
weixin_38516491
- 粉丝: 6
- 资源: 950
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0