溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解springboot WebTestClient的使用

發(fā)布時(shí)間:2020-09-20 01:57:23 來源:腳本之家 閱讀:251 作者:張占嶺 欄目:編程語言

在使用springboot進(jìn)行開發(fā)時(shí),單元測(cè)試是必要的,當(dāng)你建立一個(gè)spring項(xiàng)目時(shí),它會(huì)為我們自己生成一個(gè)測(cè)試項(xiàng)目,當(dāng)你的項(xiàng)目開始過程中,測(cè)試用例是同時(shí)要進(jìn)行的,我們?cè)谶M(jìn)行WEB層的集成測(cè)試時(shí),可以使用spring為我們提供的WebTestClient工具,非常方便,提供了基于restful的各種類型和狀態(tài)!

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

如果在你系統(tǒng)的類路徑上有Spring WebFlux,就可以選擇使用WebClient來調(diào)用遠(yuǎn)程REST服務(wù)。相比之下RestTemplate,這個(gè)客戶端具有更多的函數(shù)感并且完全Reactive響應(yīng)式的。您可以在Spring Framework文檔WebClient的專用 部分中了解有關(guān)該內(nèi)容的更多信息。

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

與RestTemplate相比,WebClient是:

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

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

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();
  }
}

本例主要用來測(cè)試響應(yīng)式的mongodb控件,其中也有一些坑,我們?cè)趓eactive-mongodb一節(jié)里再說!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI