激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解springboot WebTestClient的使用

詳解springboot WebTestClient的使用

2021-06-14 14:13張占嶺 Java教程

WebClient是一個響應式客戶端,它提供了RestTemplate的替代方法。這篇文章主要介紹了詳解springboot WebTestClient的使用, 具有一定的參考價值,感興趣的小伙伴們可以參考一下

在使用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上開源的,大叔作為總結,把它收錄在博客里。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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一節里再說!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/lori/p/8954754.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 羞羞的视频在线 | 极品大长腿啪啪高潮露脸 | 黄色免费av网站 | 免费毛片播放 | 懂色av懂色aⅴ精彩av | 国产午夜亚洲精品午夜鲁丝片 | 中国老女人一级毛片视频 | 国产成人自拍视频在线 | 中文字幕h | 999av视频 | xnxx 日本免费 | 国产精品99久久免费观看 | 全部免费毛片 | 激情综合婷婷久久 | 中文区中文字幕免费看 | 250pp久久新 黄色网址免费在线播放 | 91色琪琪电影亚洲精品久久 | 日日操夜夜操视频 | 亚洲午夜在线 | 美女毛片儿 | 毛片a区| 午夜精品成人 | 男人天堂新地址 | 欧美精品18videos性欧美 | 欧美性激情视频 | 免费看a级片 | 日韩欧美综合在线 | 国产精品久久久久久影视 | 99麻豆久久久国产精品免费 | 永久av在线免费观看 | 国产精品自在线拍 | 国产18视频 | 国产视频在线一区 | 久久久成人精品视频 | 天天看成人免费毛片视频 | 制服丝袜成人动漫 | 99久久久国产精品 | 97久久精品一区二区三区观看 | 欧美日韩成人一区二区 | 91久久国产露脸精品国产 | 欧美成人一区二区三区 |