Java实现SpringBoot对Controller的单元测试代码怎么写
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍"Java实现SpringBoot对Controller的单元测试代码怎么写",在日常操作中,相信很多人在Java实现SpringBoot对Controller的单元测试代码怎么写问题
千家信息网最后更新 2026年11月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安全错误
数据库的锁怎样保障安全
互联网科技工作委员会副主任毛军
互联网属科技
互联网品科技
港股互联网科技股
中资互联网科技
互联网科技的发展
互联网科技公司怎么样
互联网科技论文
奔汉互联网科技
互联网养老 桔豆科技
新华互联网科技校企合作
互联网科技创新企业排名
互联网汽车科技小镇
互联网科技寓意
互联网为啥属于高科技
上海宁丞科技互联网
崇明区互联网教育科技诚信经营
南通创西澳互联网科技有限公司
全球科技与互联网公司市值变化
张家口科技产业互联网平台
美国互联网保险保险科技
金诺互联网科技
互联网科技论文
互联网科技博主是什么意思
北京大学互联网科技怎么样
互联网科技实习生岗位职责
央行互联网金融科技公司
苏州五指互联网络科技有限
郑州欣桑互联网科技有限公司
黑龙江互联网科技公司