App下載

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

猿友 2021-07-17 13:46:16 瀏覽數(shù) (2057)
反饋

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 //相當(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();

 }
}

測試結(jié)果

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 代碼實(shí)現(xiàn) SpringBoot 對(duì) Controller 的單元測試以及附亂碼解決方案的全部內(nèi)容,更多相關(guān)SpringBoot以及MVC的其他內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持!


0 人點(diǎn)贊