中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

springboot~WebTestClient的(de)使用(yong)

在(zai)使(shi)用(yong)springboot進行(xing)開發(fa)時,單元測(ce)試是必要的(de),當你建(jian)立一(yi)(yi)個spring項(xiang)目時,它會(hui)為(wei)我(wo)(wo)們自(zi)己生成一(yi)(yi)個測(ce)試項(xiang)目,當你的(de)項(xiang)目開始過程中(zhong),測(ce)試用(yong)例是同時要進行(xing)的(de),我(wo)(wo)們在(zai)進行(xing)WEB層的(de)集成測(ce)試時,可以使(shi)用(yong)spring為(wei)我(wo)(wo)們提供的(de)WebTestClient工具,非(fei)常方便(bian),提供了基于restful的(de)各(ge)種類型和狀態(tai)!

下面測試用(yong)例也是(shi)spring在(zai)github上開源(yuan)的,大叔作為總結(jie),把它(ta)收(shou)錄在(zai)博客里。

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();
    }
}

本例主要用來測試響應式(shi)的mongodb控件(jian),其中(zhong)也有一些坑,我們(men)在reactive-mongodb一節里再說!

posted @ 2018-04-26 20:04  張占嶺  閱讀(6897)  評論(0)    收藏  舉報