发信息做推广,我选黔优网

发布产品信息
微信公众号 微信公众号

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

我要举报 黔优网官方账号 时间:2024-12-18 14:01 未知浏览量:22|  阅读时长:10分钟
导读: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;


@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;


@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;


@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;

 }
}

解决乱码后的效果:

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

 
  • 下一篇: 如何找回丢失的Google账号?快速恢复访问您的Google账户
  • 上一篇: 如何利用Google平台实现收益
 
没用 0举报 收藏 0评论 0
免责声明:
本页信息由“黔优网官方账号”发布,黔优网作为免费B2B信息发布平台,已对用户身份进行实名验证并对内容进行形式审核。信息的真实性、合法性由发布者独立承担全部责任,平台不承担内容准确性保证责任。本文涉及见解与观点不代表黔优网官方立场,交易决策前请务必自行核实,风险自负。原文链接:https://www.qianu.com/n/934542.html。如发现侵权或虚假内容,请【投诉举报】联系我们处理。
 
 

 
推荐图文资讯