国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

詳解springboot WebTestClient的使用

2024-07-14 08:42:51
字體:
來源:轉載
供稿:網友

在使用springboot進行開發時,單元測試是必要的,當你建立一個spring項目時,它會為我們自己生成一個測試項目,當你的項目開始過程中,測試用例是同時要進行的,我們在進行WEB層的集成測試時,可以使用spring為我們提供的WebTestClient工具,非常方便,提供了基于restful的各種類型和狀態!

WebClient是一個響應式客戶端,它提供了RestTemplate的替代方法。它公開了一個功能齊全、流暢的API,并依賴于非阻塞I / O,使其能夠比RestTemplate更高效地支持高并發性。WebClient非常適合流式的傳輸方案,并且依賴于較低級別的HTTP客戶端庫來執行請求,是可插拔的。

如果在你系統的類路徑上有Spring WebFlux,就可以選擇使用WebClient來調用遠程REST服務。相比之下RestTemplate,這個客戶端具有更多的函數感并且完全Reactive響應式的。您可以在Spring Framework文檔WebClient的專用 部分中了解有關該內容的更多信息。

WebClient使用與WebFlux服務器應用程序相同的編解碼器,并與服務器功能Web框架共享公共基本包,一些通用API和基礎結構。API公開了Reactor Flux和Mono類型。默認情況下,它使用Reactor Netty作為HTTP客戶端庫,但其他人可以通過自定義ClientHttpConnector插入。

與RestTemplate相比,WebClient是:

  • 非阻塞,Reactive的,并支持更高的并發性和更少的硬件資源。
  • 提供利用Java 8 lambdas的函數API。
  • 支持同步和異步方案。
  • 支持從服務器向上或向下流式傳輸。

下面測試用例也是spring在github上開源的,大叔作為總結,把它收錄在博客里。

package com.example.webclientdemo;import com.example.webclientdemo.payload.GithubRepo;import com.example.webclientdemo.payload.RepoRequest;import org.assertj.core.api.Assertions;import org.junit.FixMethodOrder;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.MethodSorters;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.reactive.server.WebTestClient;import reactor.core.publisher.Mono;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)@FixMethodOrder(MethodSorters.NAME_ASCENDING)public class WebclientDemoApplicationTests {  @Autowired  private WebTestClient webTestClient;  @Test  public void test1CreateGithubRepository() {    RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient");    webTestClient.post().uri("/api/repos")        .contentType(MediaType.APPLICATION_JSON_UTF8)        .accept(MediaType.APPLICATION_JSON_UTF8)        .body(Mono.just(repoRequest), RepoRequest.class)        .exchange()        .expectStatus().isOk()        .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)        .expectBody()        .jsonPath("$.name").isNotEmpty()        .jsonPath("$.name").isEqualTo("test-webclient-repository");  }  @Test  public void test2GetAllGithubRepositories() {    webTestClient.get().uri("/api/repos")        .accept(MediaType.APPLICATION_JSON_UTF8)        .exchange()        .expectStatus().isOk()        .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)        .expectBodyList(GithubRepo.class);  }  @Test  public void test3GetSingleGithubRepository() {    webTestClient.get()        .uri("/api/repos/{repo}", "test-webclient-repository")        .exchange()        .expectStatus().isOk()        .expectBody()        .consumeWith(response ->            Assertions.assertThat(response.getResponseBody()).isNotNull());  }  @Test  public void test4EditGithubRepository() {    RepoRequest newRepoDetails = new RepoRequest("updated-webclient-repository", "Updated name and description");    webTestClient.patch()        .uri("/api/repos/{repo}", "test-webclient-repository")        .contentType(MediaType.APPLICATION_JSON_UTF8)        .accept(MediaType.APPLICATION_JSON_UTF8)        .body(Mono.just(newRepoDetails), RepoRequest.class)        .exchange()        .expectStatus().isOk()        .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)        .expectBody()        .jsonPath("$.name").isEqualTo("updated-webclient-repository");  }  @Test  public void test5DeleteGithubRepository() {    webTestClient.delete()        .uri("/api/repos/{repo}", "updated-webclient-repository")        .exchange()        .expectStatus().isOk();  }}

本例主要用來測試響應式的mongodb控件,其中也有一些坑,我們在reactive-mongodb一節里再說!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 同德县| 尼勒克县| 温州市| 海门市| 定日县| 会同县| 和田市| 桐乡市| 宜都市| 神池县| 桂东县| 谷城县| 西平县| 佛冈县| 尼勒克县| 沿河| 西城区| 栾城县| 陈巴尔虎旗| 彭阳县| 盐山县| 临清市| 杭州市| 柯坪县| 江陵县| 房山区| 静安区| 南江县| 将乐县| 宁明县| 陈巴尔虎旗| 秀山| 景谷| 昌吉市| 威信县| 武邑县| 昂仁县| 奈曼旗| 马关县| 青铜峡市| 松原市|