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

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

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

瀏覽:37日期: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级在线观看国产 高清免费毛片 | 日本xxxwww色视频 | 国产一区二区三区在线观看视频 | 亚洲三级小视频 | 久久精品视频在线观看 | 99re最新网址 | 91精品欧美综合在线观看 | 日本在线国产 | 久热在线播放中文字幕 | 亚洲欧美国产精品专区久久 | 精品亚洲一区二区在线播放 | 国产精品麻豆一区二区 | 全免费观看a级毛片 | 鲁丝一区二区三区不属 | 日韩色影院| 夜色在线影院 | 特黄特色大片免费视频大全 | 国产日韩精品一区在线不卡 | 国产成人香蕉久久久久 | 国语偷拍视频在线观看 | 免费无遮挡十八女禁污污网站 | 日本一区二区三区久久 | 成人黄网大全在线观看 | 国产在线视频二区 | 国产日本韩国不卡在线视频 | 精品入口麻豆传煤 | 美国一级特级毛片片aa视频 | 高清亚洲精品酒店一区 | 美女被啪全免视频软件 | 国产高清精品入口麻豆 | 69欧美xxxxx色护士视频 | 国产污| 一级毛片国产真人永久在线 | 五月色综合婷婷综合俺来也 | 欧美亚洲日本韩国一级毛片 | 又爽又黄又无遮挡的视频美女软件 | 亚洲乱码在线 | 99在线热视频 | 国产色产综合色产在线观看视频 |