欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot對(duì)Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn)代碼 附亂碼解決方案

 更新時(shí)間:2021年04月03日 09:53:45   作者:牛哄哄的柯南  
這篇文章主要介紹了SpringBoot對(duì)Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn)代碼 附亂碼解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

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;

 }
}

單元測(cè)試代碼

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)造一個(gè) mvc對(duì)象
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();

 }
}

測(cè)試結(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對(duì)Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn)代碼 附亂碼解決方案的文章就介紹到這了,更多相關(guān)SpringBoot Controller單元測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)

    IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn)

    這篇文章主要介紹了IDEA 中創(chuàng)建SpringBoot 父子模塊的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能的實(shí)現(xiàn)代碼

    JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了JAVA中使用openoffice將Excel轉(zhuǎn)PDF再轉(zhuǎn)圖片功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • 詳解Spring Boot 使用slf4j+logback記錄日志配置

    詳解Spring Boot 使用slf4j+logback記錄日志配置

    本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • java發(fā)送內(nèi)嵌圖片郵件

    java發(fā)送內(nèi)嵌圖片郵件

    這篇文章主要介紹了java發(fā)送內(nèi)嵌圖片郵件,在博客系統(tǒng)中需要郵件服務(wù)的功能,僅僅是發(fā)送文本內(nèi)容,現(xiàn)在嘗試一下發(fā)送內(nèi)嵌圖片郵件,感興趣的小伙伴們可以參考一下
    2016-01-01
  • SpringCloud之Config配置中心與Redis分布式鎖詳解

    SpringCloud之Config配置中心與Redis分布式鎖詳解

    這篇文章主要給大家介紹了SpringCloud Alibaba中Config配置中心,Redis分布式鎖,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考閱讀
    2023-05-05
  • 深入了解MyBatis參數(shù)

    深入了解MyBatis參數(shù)

    今天小編就為大家分享一篇關(guān)于深入了解MyBatis參數(shù),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • java創(chuàng)建一個(gè)類實(shí)現(xiàn)讀取一個(gè)文件中的每一行顯示出來

    java創(chuàng)建一個(gè)類實(shí)現(xiàn)讀取一個(gè)文件中的每一行顯示出來

    下面小編就為大家?guī)硪黄猨ava創(chuàng)建一個(gè)類實(shí)現(xiàn)讀取一個(gè)文件中的每一行顯示出來的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • java?串口工具jSerialComm示例詳解

    java?串口工具jSerialComm示例詳解

    這篇文章主要介紹了java?串口工具jSerialComm,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • 詳解SpringBoot是如何保證接口安全的

    詳解SpringBoot是如何保證接口安全的

    對(duì)于互聯(lián)網(wǎng)來說,只要你系統(tǒng)的接口會(huì)暴露在外網(wǎng),就避免不了接口安全問題。?如果你的接口在外網(wǎng)裸奔,只要讓黑客知道接口的地址和參數(shù)就可以調(diào)用,那簡直就是災(zāi)難。這篇文章主要介紹了SpringBoot保證接口安全的方法,需要的可以參考一下
    2023-02-02
  • java基于mongodb實(shí)現(xiàn)分布式鎖的示例代碼

    java基于mongodb實(shí)現(xiàn)分布式鎖的示例代碼

    本文主要介紹了java基于mongodb實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論