久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:51日期: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ⅴ 羞羞的视频在线 | 欧美一区二区三区 | 日韩免费在线观看视频 | 亚洲高清在线观看 | 成人黄色在线观看 | 特级毛片在线大全免费播放 | 精品国产精品 | 中文字幕视频在线观看 | 中文字幕一区二区三区乱码图片 | 久久欧美精品一区 | 国内精品三级 | 久久国产精品久久久久久电车 | 成人亚洲网站 | 久久九 | 欧美国产视频 | 女人久久久久久久 | 久久久久久91 | 黄色a视频| av成人免费在线观看 | 欧美日韩中文国产一区发布 | 99热精品久久| 色一情一乱一伦一区二区三区 | 91午夜在线| 日本精品在线播放 | 日韩日韩日韩日韩日韩日韩日韩 | 一级毛片国产 | 黄色国产大片 | 欧美理伦片在线播放 | 欧美精品二区中文乱码字幕高清 | 亚洲成人在线视频播放 | 国产精品毛片一区二区在线看 | 亚洲精选免费视频 | 国产视频一二区 | 久久久久久亚洲 | 日韩一区二区三区在线观看 | 中文在线一区 | 看亚洲a级一级毛片 | 午夜激情免费看 | 美女扒开内裤让男人桶 | 久久在线 | 91碰碰|