spring boot单元测试之七:用mockmvc模拟session(spring boot 2.4.3)
一,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/sessiontest
2,功能说明:
演示了用mockmvc模拟有session时的操作
3,项目结构:如图:
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/27/spring-boot-dan-yuan-ce-shi-zhi-qi-yong-mockmvc-mo-ni/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,java代码说明
1,controller/UserController.java
@RestController @RequestMapping("/user") public class UserController { //读取session @GetMapping("/get") public String getsess(HttpServletRequest request) { HttpSession session=request.getSession(); String username = (String)session.getAttribute("username"); System.out.println("session username:"+username); if (username == null) { return ""; } else { return username; } } //设置session @GetMapping("/set") public String setSess(@RequestParam("userName")String userName, HttpServletRequest request) { HttpSession session=request.getSession(); session.setAttribute("username", userName); //判断写入是否成功? String nameret = (String)session.getAttribute("username"); System.out.println("set session username and read:"+nameret); return nameret; } }
2,controller/UserControllerTest.java
@AutoConfigureMockMvc @SpringBootTest class UserControllerTest { @Autowired private UserController userController; @Autowired private MockMvc mockMvc; private static MockHttpSession sessionPub; /** * 测试方法开始之前执行 设置模拟Mvc */ @BeforeAll public static void setupMockMvc() { sessionPub = new MockHttpSession(); sessionPub.setAttribute("username", "liuhongdi"); } @Test @DisplayName("测试get用户名,有session") void getTest() throws Exception { //MockHttpSession session = new MockHttpSession(); //session.setAttribute("username", "liuhongdi"); MvcResult mvcResult = mockMvc.perform(get("/user/get") .session(sessionPub) .contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andReturn(); String content = mvcResult.getResponse().getContentAsString(); assertThat(content, equalTo("liuhongdi")); } @Test @DisplayName("测试get用户名,无session") void getTestFail() throws Exception { MvcResult mvcResult = mockMvc.perform(get("/user/get") .contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andReturn(); String content = mvcResult.getResponse().getContentAsString(); assertThat(content, equalTo("")); } @Test @DisplayName("测试set session") void setTest() throws Exception { String name="mr liu"; MvcResult mvcResult = mockMvc.perform(get("/user/set?userName="+name) .session(sessionPub) .contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andReturn(); String content = mvcResult.getResponse().getContentAsString(); assertThat(content, equalTo("mr liu")); } }
三,测试效果
1,访问url查看效果:
设置session
http://127.0.0.1:8080/user/set?userName=laoliuabc
如图:
读取session:
访问:
http://127.0.0.1:8080/user/get
如图:
2,运行单元测试:
四,查看spring boot的版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.3)
分类:
spring boot单元测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2020-03-26 centos8平台使用slabtop监控slab内存的状态
2020-03-26 centos8上使用lsblk查看块设备
2020-03-26 centos8环境判断当前操作系统是否虚拟机或容器
2020-03-26 centos8平台使用lscpu查看cpu信息
2020-03-26 centos8平台使用pidstat监控cpu/内存/io