日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot测试类

發(fā)布時(shí)間:2023/12/31 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot测试类 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、SpringBoot Test介紹

Spring Test與JUnit等其他測試框架結(jié)合起來,提供了便捷高效的測試手段。而Spring Boot Test 是在Spring Test之上的再次封裝,增加了切片測試,增強(qiáng)了mock能力。

整體上,Spring Boot Test支持的測試種類,大致可以分為如下三類:

  • 單元測試:一般面向方法,編寫一般業(yè)務(wù)代碼時(shí)。涉及到的注解有@Test。
  • 切片測試:一般面向難于測試的邊界功能,介于單元測試和功能測試之間。涉及到的注解有@RunWith @WebMvcTest等。
  • 功能測試:一般面向某個(gè)完整的業(yè)務(wù)功能,同時(shí)也可以使用切面測試中的mock能力,推薦使用。涉及到的注解有@RunWith @SpringBootTest等。

功能測試過程中的幾個(gè)關(guān)鍵要素及支撐方式如下:

  • 測試運(yùn)行環(huán)境:通過@RunWith 和 @SpringBootTest啟動(dòng)spring容器。
  • mock能力:Mockito提供了強(qiáng)大mock功能。
  • 斷言能力:AssertJ、Hamcrest、JsonPath提供了強(qiáng)大的斷言能力。

二、測試示例

2.1、單元測試

import org.junit.Test;public class JavaTest {@Testpublic void test() {System.out.println(123);}

2.2、切片測試

所謂切片測試,官網(wǎng)文檔稱為 “slice” of your application,實(shí)際上是對(duì)一些特定組件的稱呼。這里的slice并非單獨(dú)的類(畢竟普通類只需要基于JUnit的單元測試即可),而是介于單元測試和集成測試中間的范圍。

比如MVC中的Controller、JDBC數(shù)據(jù)庫訪問、Redis客戶端等,這些模塊大多脫離特定環(huán)境后不能獨(dú)立運(yùn)行,假如spring沒有為此提供測試支持,開發(fā)者只能啟動(dòng)完整服務(wù)對(duì)這些模塊進(jìn)行測試,這在一些復(fù)雜的系統(tǒng)中非常不方便,所以spring為這些模塊提供了測試支持,使開發(fā)者有能力單獨(dú)對(duì)這些模塊進(jìn)行測試。

@RunWith(SpringRunner.class) @WebMvcTest(IndexController.class) public class SpringBootTest {@Autowiredprivate MockMvc mvc;@Testpublic void testExample() throws Exception {//groupManager訪問路徑//param傳入?yún)?shù)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搭配使用,可以在不啟動(dòng)web容器的情況下,對(duì)Controller進(jìn)行測試(注意:僅僅只是對(duì)controller進(jìn)行簡單的測試,如果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,下面這些類庫將被一同依賴進(jìn)去:

  • JUnit:java測試事實(shí)上的標(biāo)準(zhǔn),默認(rèn)依賴版本是4.12(JUnit5和JUnit4差別比較大,集成方式有不同)。
  • Spring Test & Spring Boot Test:Spring的測試支持。
  • AssertJ:提供了流式的斷言方式。
  • Hamcrest:提供了豐富的matcher。
  • Mockito:mock框架,可以按類型創(chuàng)建mock對(duì)象,可以根據(jù)方法參數(shù)指定特定的響應(yīng),也支持對(duì)于mock調(diào)用過程的斷言。
  • 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自動(dòng)導(dǎo)入的,版本不一致。

解決方案:更改test的版本和項(xiàng)目的spring的一致。

三、詳細(xì)API

Test Auto-configuration Annotations

SpringBoot Test 人類使用指南 - 知乎

學(xué)習(xí) Spring Boot:(二十九)Spring Boot Junit 單元測試_追光者的博客-CSDN博客

Spring Boot Test

總結(jié)

以上是生活随笔為你收集整理的SpringBoot测试类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。