亲宝软件园·资讯

展开

springboot WebTestClient 详解springboot WebTestClient的使用

张占岭 人气:0

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

下面测试用例也是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.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一节里再说!

加载全部内容

相关教程
猜你喜欢
用户评论