好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解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://HdhCmsTestcnblogs测试数据/lori/p/8954754.html

查看更多关于详解springboot WebTestClient的使用的详细内容...

  阅读:23次