在使用springboot进行开发时,单元测试是必要的,当你建立一个spring项目时,它会为我们自己生成一个测试项目,当你的项目开始过程中,测试用例是同时要进行的,我们在进行WEB层的集成测试时,可以使用spring为我们提供的WebTestClient工具,非常方便,提供了基于restful的各种类型和状态!

WebClient是一个响应式客户端,它提供了RestTemplate的替代方法。它公开了一个功能齐全、流畅的API,并依赖于非阻塞I / O,使其能够比RestTemplate更高效地支持高并发性。WebClient非常适合流式的传输方案,并且依赖于较低级别的HTTP客户端库来执行请求,是可插拔的。

如果在你系统的类路径上有Spring WebFlux,就可以选择使用WebClient来调用远程REST服务。相比之下RestTemplate,这个客户端具有更多的函数感并且完全Reactive响应式的。您可以在Spring work文档WebClient的专用 部分中了解有关该内容的更多信息。

WebClient使用与WebFlux服务器应用程序相同的编解码器,并与服务器功能Web框架共享公共基本包,一些通用API和基础结构。API公开了Reactor Flux和Mono类型。默认情况下,它使用Reactor Netty作为HTTP客户端库,但其他人可以通过自定义ClientHttpConnector插入。

与RestTemplate相比,WebClient是:

  • 非阻塞,Reactive的,并支持更高的并发性和更少的硬件资源。
  • 提供利用Java 8 lambdas的函数API。
  • 支持同步和异步方案。
  • 支持从服务器向上或向下流式传输。

下面测试用例也是spring在github上开源的,大叔作为总结,把它收录在博客里。

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.spring work.beans.factory.annotation.Autowired;
import org.spring work.boot.test.context.SpringBootTest;
import org.spring work.http.MediaType;
import org.spring work.test.context.junit4.SpringRunner;
import org.spring work.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 de ion\");
    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一节里再说!

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

收藏 打印