今天就跟大家聊聊有關(guān)怎么在SpringBoot中利用MockMvc實(shí)現(xiàn)單元測(cè)試,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

MockMvcBuilder
MockMvc是spring測(cè)試下的一個(gè)非常好用的類,他們的初始化需要在setUp中進(jìn)行。
MockMvcBuilder是用來(lái)構(gòu)造MockMvc的構(gòu)造器,其主要有兩個(gè)實(shí)現(xiàn):StandaloneMockMvcBuilder和DefaultMockMvcBuilder,前者繼承了后者。
① MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,將會(huì)從該上下文獲取相應(yīng)的控制器并得到相應(yīng)的MockMvc;
② MockMvcBuilders.standaloneSetup(Object... controllers):通過(guò)參數(shù)指定一組控制器,這樣就不需要從上下文獲取了,比如this.mockMvc = MockMvcBuilders.standaloneSetup(this.controller).build();
這些Builder還提供了其他api,可以自行百度
1.添加測(cè)試依賴
<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測(cè)試類

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)
//啟動(dòng)整個(gè)Springboot工程
@SpringBootTest(classes= {DemoApplication.class})
//鼠標(biāo)選中SpringBootTestDemo后執(zhí)行Run As -> JUnit Test可以同時(shí)執(zhí)行多個(gè)測(cè)試
@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.添加測(cè)試控制器
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.添加啟動(dòng)類
package top.ytheng.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //等于下面3個(gè)
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}看完上述內(nèi)容,你們對(duì)怎么在SpringBoot中利用MockMvc實(shí)現(xiàn)單元測(cè)試有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享標(biāo)題:怎么在SpringBoot中利用MockMvc實(shí)現(xiàn)單元測(cè)試-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://chinadenli.net/article38/dsgjpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、定制網(wǎng)站、網(wǎng)站營(yíng)銷(xiāo)、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容