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

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

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

瀏覽:59日期: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爱片特黄在线观看 一级做a爱片特黄在线观看免费看 | 成人精品| 热99re久久精品这里都是免费 | 一级毛片美国一级j毛片不卡 | 亚洲欧美综合国产不卡 | 久久久综合网 | 两性免费视频 | 日韩一级片在线免费观看 | 亚洲一级毛片欧美一级说乱 | 在线视频欧美亚洲 | 午夜欧美精品久久久久久久久 | 日本欧美一区二区三区不卡视频 | 波多结衣一区二区三区 | 国内在线精品 | 99久久精品国产免费 | 九九九九精品视频在线播放 | 91亚洲在线 | 久久久久国产 | 日韩精品一级毛片 | 91在线一区二区三区 | 精品三级网站 | 日韩欧美在线综合网高清 | 国产一区亚洲 | 亚洲一区二区中文 | 欧美色操 | 91国高清视频 | 欧美日韩高清 | 成视频年人黄网站免费 | 精品欧美一区二区在线观看欧美熟 | 国产成人精品福利网站人 | 国产精品亚洲一区在线播放 | 国产精品视频免费播放 | 中午字幕在线观看 | 日本国产在线视频 | 久久精品一区 | 久久视频这里只精品3国产 久久视频这里只有精品 | 岛国搬运工最新网地址 | 欧美激情精品久久久久 | 国产精品久久久久久影院 | 日本一线a视频免费观看 | 久久男人天堂 |