色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot對Controller進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案

瀏覽:61日期: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 //相當(dāng)于是使用 context 上下文構(gòu)造一個(gè) 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(); }}測試結(jié)果

SpringBoot對Controller進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案

亂碼解決

把注解替換為:↓@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進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案

到此這篇關(guān)于SpringBoot對Controller進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot Controller單元測試內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日本三级在线观看中文字 | 国产成人a大片大片在线播放 | 成人影院午夜久久影院 | 国产精品合集一区二区 | 精品国产自在现线看久久 | 亚洲b | 国产精品中文 | 香蕉521av网站永久地址 | 久久青草国产手机看片福利盒子 | 91精品国产综合久久久久 | 男人好大好硬好爽免费视频 | 怡红院爽妇网 | 久久视频精品53在线观看 | 国产精品国产三级国产专区5o | 免费一看一级毛片全播放 | 99国产在线 | 成人在线视频国产 | 一级视频免费观看 | 亚洲成a人片在线观 | 在线亚洲精品中文字幕美乳 | 99精品国产免费久久国语 | 亚洲一级毛片免观看 | 日韩中文字幕免费 | 最新理论三级中文在线观看 | 中文字幕国产一区 | 99九九精品国产高清自在线 | 久草在线视频精品 | 美国免费一级片 | 久久99久久精品免费思思 | 欧美成人免费高清网站 | 国产欧美精品一区二区三区四区 | 欧美丰满大乳大屁股毛片 | 亚洲美女在线观看播放 | a大片久久爱一级 | 91av综合| 久久久久综合给合狠狠狠 | 中文字幕精品一区二区精品 | 亚洲免费在线播放 | 91国偷自产一区二区三区 | 人操人碰| 国产成人精品综合在线 |