当前位置:
首页
文章
后端
详情

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

MVC 模式是设计一个程序的架构模式之一,是当前项目开发中最流行的架构模式之一。本文将介绍 SpringBoot 对 MVC 中的Controller 进行单元测试,并给出Java实现代码,以及关于乱码解决的方案。

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();

 }
}

测试结果

20210403094816106

乱码解决

把注解替换为:↓
@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;

 }
}

解决乱码后的效果:

20210403094816107

以上就是使用 Java 代码实现 SpringBoot 对 Controller 的单元测试以及附乱码解决方案的全部内容,更多相关SpringBoot以及MVC的其他内容请搜索W3Cschool以前的文章或继续浏览下面的相关文章,希望大家以后多多支持!


免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。

同类热门文章

深入了解C++中的new操作符:使用具体实例学习

C++中的new操作符是动态分配内存的主要手段之一。在程序运行时,我们可能需要动态地创建和销毁对象,而new就是为此提供了便利。但是,使用new也常常会引发一些问题,如内存泄漏、空指针等等。因此,本文将通过具体的示例,深入介绍C++中的new操作符,帮助读者更好地掌握其使用。


深入了解C++中的new操作符:使用具体实例学习

怎么用Java反射获取包下所有类? 详细代码实例操作

Java的反射机制就是在运行状态下,对于任何一个类,它能知道这个类的所有属性和方法;对于任何一个对象,都能调用这个对象的任意一个方法。本篇文章将通过具体的代码示例,展示如何通过Java反射来获取包下的所有类。


怎么用Java反射获取包下所有类? 详细代码实例操作

了解Java中的volati关键字的作用 以及具体使用方法

本篇文章将和大家分享一下Java当中的volatile关键字,下面将为各位小伙伴讲述volatile关键字的作用以及它的具体使用方法。


了解Java中的volati关键字的作用 以及具体使用方法

Java Map 所有的值转为String类型

可以使用 Java 8 中的 Map.replaceAll() 方法将所有的值转为 String 类型: 上面的代码会将 map 中所有的值都转为 String 类型。 HashMap 是 Java

Java Map 所有的值转为String类型

员工线上学习考试系统

有点播,直播,在线支付,三级分销等功能,可以对学员学习情况的监督监控,有源码,可二次开发。支持外网和局域网私有化部署,经过测试源码完整可用!1、视频点播:视频播放,图文资料,课件下载,章节试学,限时免

员工线上学习考试系统