欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

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è)試類

怎么在SpringBoot中利用MockMvc實(shí)現(xiàn)單元測(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)

綿陽(yáng)服務(wù)器托管