当前位置 博文首页 > 文章内容

    SpringBoot对Controller进行单元测试的实现代码 附乱码解决方案

    作者:shunshunshun18 栏目:未分类 时间:2021-04-03 14:43:12

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    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 //相当于是使用 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.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;
    
     }
    }

    解决乱码后的效果:

    在这里插入图片描述