1. 程式人生 > >SpringBootTest單元測試實戰、SpringBoot測試進階高級篇之MockMvc講解

SpringBootTest單元測試實戰、SpringBoot測試進階高級篇之MockMvc講解

dep out string ltm shm let 如果 frame group

1、@SpringBootTest單元測試實戰

簡介:講解SpringBoot的單元測試

1、引入相關依賴

<!--springboot程序測試依賴,如果是自動創建項目默認添加-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

2、使用

       測試類:

@RunWith(SpringRunner.class) //底層用junit SpringJUnit4ClassRunner

@SpringBootTest(classes={XdclassApplication.class})//啟動整個springboot工程

public class SpringBootTests { }

      

2、SpringBoot測試進階高級篇之MockMvc講解

簡介:講解MockMvc類的使用和模擬Http請求實戰

1、增加類註解 @AutoConfigureMockMvc

@SpringBootTest(classes={XdclassApplication.class})

2、相關API

perform:執行一個RequestBuilder請求

andExpect:添加ResultMatcher->MockMvcResultMatchers驗證規則

andReturn:最後返回相應的MvcResult->Response

    代碼示例:

    SampleControler.java:

 1 package net.xdclass.demo.controller;
 2 
 3 import java.util.Date;
 4 import
java.util.HashMap; 5 import java.util.Map; 6 7 import org.springframework.boot.*; 8 import org.springframework.boot.autoconfigure.*; 9 import org.springframework.web.bind.annotation.*; 10 11 import net.xdclass.demo.domain.User; 12 13 @RestController 14 public class SampleControler { 15 16 @RequestMapping("/test/home") 17 public String home() { 18 return "xdclass"; 19 } 20 21 }

    測試:

    MockMvcTestDemo.java:

 1 package xdclass_springboot.demo;
 2 
 3 import net.xdclass.demo.XdClassApplication;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.context.junit4.SpringRunner;
11 import org.springframework.test.web.servlet.MockMvc;
12 import org.springframework.test.web.servlet.MvcResult;
13 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14 import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
15 
16 
17 
18 /**
19  * 功能描述:測試mockmvc類
20  */
21 @RunWith(SpringRunner.class)  //底層用junit  SpringJUnit4ClassRunner
22 @SpringBootTest(classes={XdClassApplication.class}) //啟動整個springboot工程
23 @AutoConfigureMockMvc 
24 public class MockMvcTestDemo {
25 
26     
27     @Autowired
28     private MockMvc mockMvc;
29     
30     
31     
32     @Test
33     public void apiTest() throws Exception {
34         
35         MvcResult mvcResult =  mockMvc.perform( MockMvcRequestBuilders.get("/test/home_xxx") ).
36                 andExpect( MockMvcResultMatchers.status().isOk() ).andReturn();
37         int status = mvcResult.getResponse().getStatus();
38         System.out.println(status);
39         
40     }
41     
42 }

1、@SpringBootTest單元測試實戰簡介:講解SpringBoot的單元測試1、引入相關依賴 <!--springboot程序測試依賴,如果是自動創建項目默認添加--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

2、使用@RunWith(SpringRunner.class) //底層用junit SpringJUnit4ClassRunner@SpringBootTest(classes={XdclassApplication.class})//啟動整個springboot工程public class SpringBootTests { }

SpringBootTest單元測試實戰、SpringBoot測試進階高級篇之MockMvc講解