怎么在SpringBoot中利用MockMvc實現(xiàn)單元測試-創(chuàng)新互聯(lián)

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

10多年的江岸網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整江岸建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“江岸網(wǎng)站設(shè)計”,“江岸網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

MockMvcBuilder

MockMvc是spring測試下的一個非常好用的類,他們的初始化需要在setUp中進行。

MockMvcBuilder是用來構(gòu)造MockMvc的構(gòu)造器,其主要有兩個實現(xiàn):StandaloneMockMvcBuilder和DefaultMockMvcBuilder,前者繼承了后者。

① MockMvcBuilders.webAppContextSetup(WebApplicationContext context):指定WebApplicationContext,將會從該上下文獲取相應的控制器并得到相應的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測試類

怎么在SpringBoot中利用MockMvc實現(xiàn)單元測試

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})
//鼠標選中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);
  }
}

看完上述內(nèi)容,你們對怎么在SpringBoot中利用MockMvc實現(xiàn)單元測試有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享標題:怎么在SpringBoot中利用MockMvc實現(xiàn)單元測試-創(chuàng)新互聯(lián)
標題鏈接:http://muchs.cn/article38/dsgjpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、定制網(wǎng)站網(wǎng)站營銷、關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計公司、網(wǎng)站導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管