javascript
SpringBoot测试类
一、SpringBoot Test介紹
Spring Test與JUnit等其他測試框架結合起來,提供了便捷高效的測試手段。而Spring Boot Test 是在Spring Test之上的再次封裝,增加了切片測試,增強了mock能力。
整體上,Spring Boot Test支持的測試種類,大致可以分為如下三類:
- 單元測試:一般面向方法,編寫一般業務代碼時。涉及到的注解有@Test。
- 切片測試:一般面向難于測試的邊界功能,介于單元測試和功能測試之間。涉及到的注解有@RunWith @WebMvcTest等。
- 功能測試:一般面向某個完整的業務功能,同時也可以使用切面測試中的mock能力,推薦使用。涉及到的注解有@RunWith @SpringBootTest等。
功能測試過程中的幾個關鍵要素及支撐方式如下:
- 測試運行環境:通過@RunWith 和 @SpringBootTest啟動spring容器。
- mock能力:Mockito提供了強大mock功能。
- 斷言能力:AssertJ、Hamcrest、JsonPath提供了強大的斷言能力。
二、測試示例
2.1、單元測試
import org.junit.Test;public class JavaTest {@Testpublic void test() {System.out.println(123);}2.2、切片測試
所謂切片測試,官網文檔稱為 “slice” of your application,實際上是對一些特定組件的稱呼。這里的slice并非單獨的類(畢竟普通類只需要基于JUnit的單元測試即可),而是介于單元測試和集成測試中間的范圍。
比如MVC中的Controller、JDBC數據庫訪問、Redis客戶端等,這些模塊大多脫離特定環境后不能獨立運行,假如spring沒有為此提供測試支持,開發者只能啟動完整服務對這些模塊進行測試,這在一些復雜的系統中非常不方便,所以spring為這些模塊提供了測試支持,使開發者有能力單獨對這些模塊進行測試。
@RunWith(SpringRunner.class) @WebMvcTest(IndexController.class) public class SpringBootTest {@Autowiredprivate MockMvc mvc;@Testpublic void testExample() throws Exception {//groupManager訪問路徑//param傳入參數MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/groupManager").param("pageNum","1").param("pageSize","10")).andReturn();MockHttpServletResponse response = result.getResponse();String content = response.getContentAsString();List<JtInfoDto> jtInfoDtoList = GsonUtils.toObjects(content, new TypeToken<List<JtInfoDto>>() {}.getType());for(JtInfoDto infoDto : jtInfoDtoList){System.out.println(infoDto.getJtCode());}} }使用@WebMvcTest和MockMvc搭配使用,可以在不啟動web容器的情況下,對Controller進行測試(注意:僅僅只是對controller進行簡單的測試,如果Controller中依賴用@Autowired注入的service、dao等則不能這樣測試)。
2.3、功能測試?
POM引入:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version><scope>compile</scope> </dependency> <dependency><groupId>junit</groupId><artifactId>junit</artifactId> </dependency>?一旦依賴了spring-boot-starter-test,下面這些類庫將被一同依賴進去:
- JUnit:java測試事實上的標準,默認依賴版本是4.12(JUnit5和JUnit4差別比較大,集成方式有不同)。
- Spring Test & Spring Boot Test:Spring的測試支持。
- AssertJ:提供了流式的斷言方式。
- Hamcrest:提供了豐富的matcher。
- Mockito:mock框架,可以按類型創建mock對象,可以根據方法參數指定特定的響應,也支持對于mock調用過程的斷言。
- JSONassert:為JSON提供了斷言功能。
- JsonPath:為JSON提供了XPATH功能
SpringBoot測試類:?
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootTest {@Autowiredprivate DiscoveryClient discoveryClient;@Testpublic void NacosTest() {List<String> services = discoveryClient.getServices();services.forEach(x-> System.out.println(x));} }FAQ:Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
異常原因:test的包是idea自動導入的,版本不一致。
解決方案:更改test的版本和項目的spring的一致。
三、詳細API
Test Auto-configuration Annotations
SpringBoot Test 人類使用指南 - 知乎
學習 Spring Boot:(二十九)Spring Boot Junit 單元測試_追光者的博客-CSDN博客
Spring Boot Test
總結
以上是生活随笔為你收集整理的SpringBoot测试类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装Win10操作系统-纯净版
- 下一篇: 如何优化你的ERP库存管理系统