Java实现SpringBoot对Controller的单元测试代码怎么写
发表于:2025-11-20 作者:千家信息网编辑
千家信息网最后更新 2025年11月20日,这篇文章主要介绍"Java实现SpringBoot对Controller的单元测试代码怎么写",在日常操作中,相信很多人在Java实现SpringBoot对Controller的单元测试代码怎么写问题
千家信息网最后更新 2025年11月20日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安全错误
数据库的锁怎样保障安全
主机服务器商管理系统
服务器租用代理
数据库应用系统开发是干嘛的
关于网络安全法的资料
立象打印机怎么连接数据库
甘肃服务器虚拟化操作云主机
was服务器查看项目
通过看项目写数据库
网上买东西老是服务器错误
数据库产品哪个好
网络技术是web吗
怎么导入数据库2005
杭州攻克网络安全难题计划
台州企业软件开发工具
互联网 科技展板
网吧游戏服务器长什么样
be服务器未正常启动
软件开发烧脑子吗
wifi服务器ip在哪看
黄山股份公司软件开发部好吗
山东丰豪无限互联网科技
反欺诈数据库命中客户
基站定位数据库 下载
网络技术出来是做什么的
网络技术是web吗
游戏服务器的使用量
网络安全监管审查
prism ark服务器
嵌入式系统使用什么数据库
唐毅网络安全