亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot對Controller進行單元測試的實現代碼 附亂碼解決方案

瀏覽:60日期:2023-03-19 10:48:35
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(); }}測試結果

SpringBoot對Controller進行單元測試的實現代碼 附亂碼解決方案

亂碼解決

把注解替換為:↓@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; }}

解決亂碼后的效果:

SpringBoot對Controller進行單元測試的實現代碼 附亂碼解決方案

到此這篇關于SpringBoot對Controller進行單元測試的實現代碼 附亂碼解決方案的文章就介紹到這了,更多相關SpringBoot Controller單元測試內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 精品视频在线观看视频免费视频 | 亚洲第一视频在线观看 | 国产黄色免费 | 最新国产在线视频 | 超污视频网站 | 欧美嘿咻视频 | 一级黄色录像毛片 | 亚洲一区二区三 | 毛片在线播放观看日本 | 一级做a爱过程免费视频麻豆 | 亚洲欧美另类在线 | 一级一级 a爱片免费视频 | 黄色a视频在线观看 | 成人啪啪网站 | 永久免费观看午夜视频在线 | 亚洲一区二区在线免费观看 | 一级做a爰片性色毛片中国 一级做a爰片性色毛片男 | 亚洲邪恶| 伊人影院视频 | 日日噜噜噜夜夜爽爽狠狠 | 日韩性网 | 高清在线不卡 | 曰批美女免费视频播放 | 国产精品4p露脸在线播放 | 99精品国产三级在线观看 | 国产尤物在线播放 | 国产精品无码2021在线观看 | 亚洲日日做天天做日日谢 | 亚洲欧美日本国产综合在线 | 黄视频免费在线看 | 在线视频 日韩 | 日韩黑寡妇一级毛片国语对白 | 美国一级黄色 | 国产福利在线视频 | 亚洲二区在线观看 | 亚洲精品综合久久 | 免费精品99久久国产综合精品 | 亚洲国产九九精品一区二区 | 手机看片1024久久香蕉 | 日韩一区二区久久久久久 | 男人粗大一出一进女人下面视频 |