uniapp开发app框架在提升开发效率中的独特优势与应用探索
554
2023-01-31
SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案
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-/
*/
@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;http://
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-/
*/
@SpringBootTest(classes = Sprinhttp://gBoot02Application.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();
}
}
测试结果
乱码解决
把注解替换为:↓
@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-/
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello",produces = {"application/json;charset=UTF-8"})
//@RequestMapping("/hello")
Map hello(){
Map map = new HashMap();
aHWiqjFXX map.put("keafmd","牛哄哄的柯南");
map.put("success",true);
return map;
}
}
解决乱码后的效果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~