SpringBoot對(duì)Controller進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案
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/
*/
@RestController
public 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對(duì)象
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é)果

亂碼解決
把注解替換為:↓
@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/
*/
@RestController
public 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;
}
}
解決亂碼后的效果:

到此這篇關(guān)于SpringBoot對(duì)Controller進(jìn)行單元測試的實(shí)現(xiàn)代碼 附亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot Controller單元測試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
log4j如何根據(jù)變量動(dòng)態(tài)生成文件名
這篇文章主要介紹了log4j如何根據(jù)變量動(dòng)態(tài)生成文件名方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
JAVA復(fù)制數(shù)組和重置數(shù)組大小操作
這篇文章主要介紹了JAVA復(fù)制數(shù)組和重置數(shù)組大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java實(shí)現(xiàn)生成pdf并解決表格分割的問題
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)生成pdf,并解決表格分割的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫分表
這篇文章主要介紹了SpringBoot 2.0 整合sharding-jdbc中間件,實(shí)現(xiàn)數(shù)據(jù)分庫分表,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
SpringBoot 過濾器 Filter使用實(shí)例詳解
這篇文章主要為大家介紹了SpringBoot 過濾器Filter使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java?IO流之StringWriter和StringReader用法分析
這篇文章主要介紹了Java?IO流之StringWriter和StringReader用法分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

