SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案
作者:牛哄哄的柯南 时间:2021-11-26 17:57:22
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/
*/
@RestController
public 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/
*/
@RestController
public 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;
}
}
解决乱码后的效果:
来源:https://keafmd.blog.csdn.net/article/details/115395273
标签:SpringBoot,Controller,单元测试,乱码
0
投稿
猜你喜欢
图文详解Java中的字节输入与输出流
2022-12-23 23:14:48
idea2020.3.3集成maven及遇到的坑(推荐)
2021-11-18 04:18:54
SpringBoot使用注解进行分页的实现示例
2021-08-21 22:45:32
基于springmvc之常用注解,操作传入参数
2023-03-17 20:49:52
Java Arrays.asList使用方法解析
2023-11-26 06:27:09
Java数据结构之优先级队列(PriorityQueue)用法详解
2023-11-18 13:00:50
TOMCAT内存溢出及大小调整的实现方法
2023-02-24 06:55:33
java实现多线程文件的断点续传
2023-11-19 22:42:05
java图片缩放实现图片填充整个屏幕
2021-06-09 18:30:07
idea全局搜索快捷键超详细总结(推荐!)
2021-08-12 20:16:18
java基于JDBC连接Oracle 11g Release2实例分析
2022-06-06 02:36:36
SpringBoot如何整合redis实现过期key监听事件
2023-08-04 18:51:19
分布式调度XXL-Job整合Springboot2.X实战操作过程(推荐)
2023-11-23 09:43:38
RocketMQ生产者一个应用不能发送多个NameServer消息解决
2022-05-18 15:56:11
C# 解析XML和反序列化的示例
2023-02-20 01:16:52
java求最大公约数与最小公倍数的方法示例
2023-01-23 03:28:58
ArrayList的自动扩充机制实例解析
2021-10-20 17:38:29
带你了解Spring中bean的获取
2021-10-10 09:53:10
sharding-jdbc5.0.0实现分表实践
2023-12-07 10:12:26
Java方法的参数传递机制实例详解
2021-05-24 07:38:29