国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(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 | 看真人视频一级毛片 | 成人在线手机视频 | 亚洲香蕉久久一区二区三区四区 | a视频免费| 欧美日韩亚洲高清不卡一区二区三区 | 亚洲自拍小视频 | 日韩一级片免费 | 国产自约视频 | 日本一级毛片在线看 | 国产亚洲欧美久久精品 | 精品精品国产自在久久高清 | 国产精品一区二区三区免费 | 久草网在线视频 | 亚洲狠狠狠一区二区三区 | 国产做a爰片久久毛片a | 精品国产香蕉在线播出 | 看成年女人免费午夜视频 | 日韩欧美视频在线一区二区 | 久香草视频在线观看免费 | 欧美激情欧美狂野欧美精品免费 | 亚洲视频在线播放 | 国产一二区 | a级黄色毛片免费播放视频 a级精品九九九大片免费看 | 一级做a爰片性色毛片视频图片 | 午夜黄色福利视频 | 九九大香尹人视频免费 | 久久综合色88 | 国产精品自在线天天看片 | 中文字幕精品一区二区三区视频 | 亚洲精品国产成人一区二区 | 成免费网站 | 一级特黄一欧美俄罗斯毛片 | 欧美亚洲在线 | 国产真实乱子伦xxxxchina | 国产精品色午夜视频免费看 | 欧美日韩久久 |