在使用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