国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-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)文章:
主站蜘蛛池模板: 成人免费网站在线观看 | 国产日韩精品视频一区二区三区 | 97精品国产91久久久久久 | 欧美三级网站在线观看 | 精品成人 | 国产毛片一区 | 国产成人免费观看 | 精品视频一区二区三三区四区 | 成人首页 | 国产成人综合久久精品红 | 国产高颜值露脸在线观看 | 亚洲精品国产第一区二区多人 | 娇喘嗯嗯~轻点啊视频福利 | 女人张开双腿让男人桶爽免 | 97视频免费播放观看在线视频 | 国产精品一级视频 | 99久久精品免费看国产一区二区三区 | 婷婷91 | 亚洲精品高清国产麻豆专区 | 国产性生活视频 | 亚洲国产成人久久一区www | 图片区偷拍区小说区 | 一级片免费观看 | 国产剧情一区二区 | 日韩欧美国产精品 | 欧美日韩一区二区综合在线视频 | 国产成人综合精品一区 | 91理论片午午伦夜理片久久 | 欧美精品免费在线 | 国产一区二区免费播放 | 在线看免费观看韩国特黄一级 | 精品亚洲一区二区三区 | 国内自拍tv在线 | 亚洲久草 | 欧美a级在线观看 | 国产综合久久一区二区三区 | 91成人国产网站在线观看 | 精品视频一区二区 | 国产欧美在线不卡 | 日韩欧免费一区二区三区 | 久草热视频在线观看 |