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

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

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
相關文章:
主站蜘蛛池模板: 免费一级毛片在线播放不收费 | 亚洲综合在线最大成人 | 国产一区二区不卡视频 | 美国一级特黄aa大片 | 国产中文字幕第一页 | free性欧美chinese乱子 | 中文字幕第四页 | 91视频第一页 | 亚洲欧美一级夜夜爽w | 久久综合久久综合九色 | 一区影院 | 国产色综合天天综合网 | 国产精品丝袜 | 国产精品视频人人做人人爱 | 黄色三级视频在线 | 国产不卡免费视频 | 久久成人性色生活片 | 国产永久免费高清动作片www | 国产精品亚洲第一区焦香 | 久久日本精品一区二区三区 | 狼人香蕉国产在线视频 | 亚洲小视频在线观看 | 亚洲第一区精品观看 | 亚洲人成网国产最新在线 | 国产爆操 | 一级女人毛片人一女人 | 欧美日韩一区二区三区毛片 | 日本一级级特黄特色大片 | 亚洲一区日韩二区欧美三区 | 免费人成网站尤物在线观看 | 国产欧美一区二区精品性色99 | 欧美精品一区二区三区观 | 在线日韩中文字幕 | 国产成人19禁在线观看 | 美女美女高清毛片视频 | 7777sq国产精品 | 欧美一区二区三区在观看 | 久久伊人青青 | 一级黄色性感片 | 欧美专区一区 | 国产a级黄色毛片 |