Java实现SpringBoot对Controller的单元测试代码怎么写
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍"Java实现SpringBoot对Controller的单元测试代码怎么写",在日常操作中,相信很多人在Java实现SpringBoot对Controller的单元测试代码怎么写问题
千家信息网最后更新 2025年02月03日Java实现SpringBoot对Controller的单元测试代码怎么写
这篇文章主要介绍"Java实现SpringBoot对Controller的单元测试代码怎么写",在日常操作中,相信很多人在Java实现SpringBoot对Controller的单元测试代码怎么写问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java实现SpringBoot对Controller的单元测试代码怎么写"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Controller代码
package com.keafmd.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * Keafmd * * @ClassName: HelloController * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-02 9:42 * @Blog: https://keafmd.blog.csdn.net/ */@RestControllerpublic class HelloController { @RequestMapping("/hello") Map hello(){ Map map = new HashMap(); map.put("keafmd","牛哄哄的柯南"); map.put("success",true); return map; }}
单元测试代码
package com.keafmd;import com.keafmd.SpringBoot02Application;import com.keafmd.controller.HelloController;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;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.test.context.ContextConfiguration;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;/** * Keafmd * * @ClassName: MvcTest * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-02 10:59 * @Blog: https://keafmd.blog.csdn.net/ */@SpringBootTest(classes = SpringBoot02Application.class)@AutoConfigureMockMvc //相当于是使用 context 上下文构造一个 mvc对象public class MvcTest { //模拟访问 Controller @Autowired MockMvc mvc; @Test public void test() throws Exception { MvcResult result = mvc.perform( MockMvcRequestBuilders.get("/hello"). accept(MediaType.APPLICATION_JSON)). andExpect(MockMvcResultMatchers.status().isOk()). andDo(MockMvcResultHandlers.print()).andReturn(); }}
乱码解决
把注解替换为:↓@RequestMapping(value = "/hello",produces = {"application/json;charset=UTF-8"})
HelloController:
package com.keafmd.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * Keafmd * * @ClassName: HelloController * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-02 9:42 * @Blog: https://keafmd.blog.csdn.net/ */@RestControllerpublic class HelloController { @RequestMapping(value = "/hello",produces = {"application/json;charset=UTF-8"}) //@RequestMapping("/hello") Map hello(){ Map map = new HashMap(); map.put("keafmd","牛哄哄的柯南"); map.put("success",true); return map; }}
到此,关于"Java实现SpringBoot对Controller的单元测试代码怎么写"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
代码
单元
测试
哄哄
柯南
学习
更多
帮助
实用
接下来
上下
上下文
乱码
对象
文章
方法
注解
理论
知识
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库系统为什么复杂
数据库技术与应用答案Sql
软件开发工资为什么这么高
电信日网络安全宣传周
菜鸟要学的数据库基础知识
学生网络安全手则
富文本保存到数据库
软件开发自学吗
相框制作软件开发
网络安全进入校园防范风险
网络安全配置包括哪些内容
关注网络安全的知识
广东pdu服务器电源哪家靠谱
期刊数据库收录的斯苗人
沈阳眼医互联网科技
数据库质疑数据源
google找不到服务器id
数据库主文件可以有几个
西安中国银行软件开发中心
梅州通讯软件开发价目表
服务器定期重启有没有好处
forge服务器进不去
互联网网络技术服务
华为移动服务器安装包
软件开发转让咨询
excel中粘贴筛选数据库
网络技术用ip地址获取域名
向日葵提示被控告服务器连接失败
数据访问必须通过数据库管理系统
广州网络安全峰会口译