详解springboot WebTestClient的使用

2025-05-29 0 61

在使用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一节里再说!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解springboot WebTestClient的使用 https://www.kuaiidc.com/110466.html

相关文章

发表评论
暂无评论