午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

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

瀏覽:160日期: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)文章:
主站蜘蛛池模板: 成年人黄色一级片 | 综合第一页| 一区av在线 | 蜜桃传媒一区二区 | 天天干天天做 | 亚洲人成小说 | 久久久久久久久久免费视频 | 这里只有精品视频 | 97啪啪| 日本天堂中文字幕 | 国产精品99久久久久 | 中文天堂在线资源 | 六月综合 | 亚洲第一av| 五月在线| 欧美三级欧美一级 | 九一在线免费观看 | 四房婷婷| 亚洲羞羞 | 一级黄色片毛片 | 日韩精品视频观看 | 精品1区2区 | 国产在线毛片 | 黄色一级一级 | 女人毛片视频 | 国产黄色成人 | 日韩在线观看视频一区二区 | 久久久久黄色片 | 日本成人午夜视频 | 欧美极品一区 | 国产一区二区三区四 | 欧美精品久久久久久久 | 亚洲激情国产 | 亚洲伊人影院 | 日本成人一区二区三区 | 欧美毛片网站 | 四虎影院永久在线 | 日韩中文字幕在线 | 成年人视频网站 | 向井蓝在线观看 | 欧美美女一区二区 |