SpringBoot MockMvc單元測試的示例代碼
為何使用MockMvc?
對模塊進(jìn)行集成測試時,希望能夠通過輸入URL對Controller進(jìn)行測試,如果通過啟動服務(wù)器,建立http client進(jìn)行測試,這樣會使得測試變得很麻煩,比如,啟動速度慢,測試驗(yàn)證不方便,依賴網(wǎng)絡(luò)環(huán)境等,所以為了可以對Controller進(jìn)行測試,我們引入了MockMVC。
MockMvc實(shí)現(xiàn)了對Http請求的模擬,能夠直接使用網(wǎng)絡(luò)的形式,轉(zhuǎn)換到Controller的調(diào)用,這樣可以使得測試速度快、不依賴網(wǎng)絡(luò)環(huán)境,而且提供了一套驗(yàn)證的工具,這樣可以使得請求的驗(yàn)證統(tǒng)一而且很方便。
MockMvcBuilder
MockMvc是spring測試下的一個非常好用的類,他們的初始化需要在setUp中進(jìn)行。
MockMvcBuilder是用來構(gòu)造MockMvc的構(gòu)造器,其主要有兩個實(shí)現(xiàn):StandaloneMockMvcBuilder和DefaultMockMvcBuilder,前者繼承了后者。
① MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,將會從該上下文獲取相應(yīng)的控制器并得到相應(yīng)的MockMvc;
② MockMvcBuilders.standaloneSetup(Object... controllers):通過參數(shù)指定一組控制器,這樣就不需要從上下文獲取了,比如this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
這些Builder還提供了其他api,可以自行百度
1.添加測試依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.ytheng</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.添加MocMvc測試類

package top.ytheng.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
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.test.context.junit4.SpringRunner;
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.MockMvcResultMatchers;
//底層用junit SpringJunit4ClassRunner
@RunWith(SpringRunner.class)
//啟動整個Springboot工程
@SpringBootTest(classes= {DemoApplication.class})
//鼠標(biāo)選中SpringBootTestDemo后執(zhí)行Run As -> JUnit Test可以同時執(zhí)行多個測試
@AutoConfigureMockMvc
public class MockMvcTestDemo {
@Autowired
private MockMvc mockMvc;
@Test
public void mockTest() throws Exception {
MvcResult mockResult = mockMvc.perform(MockMvcRequestBuilders.get("/file/testpath")).
andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
int status = mockResult.getResponse().getStatus();
System.out.println(status);
}
}
3.添加測試控制器
package top.ytheng.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/file")
public class FileController {
@RequestMapping("/testpath")
@ResponseBody
private Object testPath() {
return filePath;
}
}
4.添加啟動類
package top.ytheng.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //等于下面3個
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5.鼠標(biāo)選中MockMvcTestDemo類名,使用快捷鍵啟動JUnit測試(Shift + Alt + X,T),進(jìn)行測試
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java調(diào)用打印機(jī)的2種方式舉例(無驅(qū)/有驅(qū))
我們平時使用某些軟件或者在超市購物的時候都會發(fā)現(xiàn)可以使用打印機(jī)進(jìn)行打印,這篇文章主要給大家介紹了關(guān)于Java調(diào)用打印機(jī)的2種方式,分別是無驅(qū)/有驅(qū)的相關(guān)資料,需要的朋友可以參考下2023-11-11
Java二維數(shù)組與稀疏數(shù)組相互轉(zhuǎn)換實(shí)現(xiàn)詳解
在某些應(yīng)用場景中需要大量的二維數(shù)組來進(jìn)行數(shù)據(jù)存儲,但是二維數(shù)組中卻有著大量的無用的位置占據(jù)著內(nèi)存空間,稀疏數(shù)組就是為了優(yōu)化二維數(shù)組,節(jié)省內(nèi)存空間2022-09-09
阿里的Easyexcel讀取Excel文件的方法(最新版本)
這篇文章主要介紹了阿里的Easyexcel讀取Excel文件(最新版本)的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Java Robot應(yīng)用示例之機(jī)器人功能
這篇文章主要為大家詳細(xì)介紹了Java Robot應(yīng)用示例之機(jī)器人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Java中@Autowired和@Resource區(qū)別
本文主要介紹了Java中@Autowired和@Resource區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java spring boot 實(shí)現(xiàn)支付寶支付功能的示例代碼
這篇文章主要介紹了Java spring boot 實(shí)現(xiàn)支付寶支付功能,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

