溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)SpringBoot對(duì)Controller的單元測(cè)試代碼怎么寫

發(fā)布時(shí)間:2022-02-24 10:00:05 來(lái)源:億速云 閱讀:482 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java實(shí)現(xiàn)SpringBoot對(duì)Controller的單元測(cè)試代碼怎么寫”,在日常操作中,相信很多人在Java實(shí)現(xiàn)SpringBoot對(duì)Controller的單元測(cè)試代碼怎么寫問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java實(shí)現(xiàn)SpringBoot對(duì)Controller的單元測(cè)試代碼怎么寫”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

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 {

 //模擬訪問(wèn) 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;

 }
}

到此,關(guān)于“Java實(shí)現(xiàn)SpringBoot對(duì)Controller的單元測(cè)試代碼怎么寫”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI