SpringBoot對Controller進(jìn)行單元測試的實現(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)造一個 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é)果

亂碼解決
把注解替換為:↓
@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對Controller進(jìn)行單元測試的實現(xiàn)代碼 附亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot Controller單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA 中創(chuàng)建SpringBoot 父子模塊的實現(xiàn)
這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能的實現(xiàn)代碼
這篇文章主要介紹了JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
詳解Spring Boot 使用slf4j+logback記錄日志配置
本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
SpringCloud之Config配置中心與Redis分布式鎖詳解
這篇文章主要給大家介紹了SpringCloud Alibaba中Config配置中心,Redis分布式鎖,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考閱讀2023-05-05
java創(chuàng)建一個類實現(xiàn)讀取一個文件中的每一行顯示出來
下面小編就為大家?guī)硪黄猨ava創(chuàng)建一個類實現(xiàn)讀取一個文件中的每一行顯示出來的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
java基于mongodb實現(xiàn)分布式鎖的示例代碼
本文主要介紹了java基于mongodb實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

