簡體   English   中英

春季啟動WebTestClient全面集成測試

[英]spring boot WebTestClient full integration test

我想使用WebTestClient執行完整的集成測試。
我的測試很簡單:

@Test
public void createPostTest() {
    Post post = new Post("someUserId", "Kim", "Gysen",
            "Some text", "http://zwoop.be/imagenr",
            "googlePlaceId", "googlePlaceName", new Geolocation(50, 50));

    webTestClient.post().uri(BASE_URI)
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .body(Mono.just(post), Post.class)
            .exchange()
            .expectStatus().isCreated()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
            .expectBody()
            .jsonPath("$._id").isNotEmpty()
            .jsonPath("$.userId").isEqualTo(post.getUserId())
            .jsonPath("$.firstName").isEqualTo(post.getFirstName())
            .jsonPath("$.lastName").isEqualTo(post.getLastName())
            .jsonPath("$.postText").isEqualTo(post.getPostText())
            .jsonPath("$.postImageUri").isEqualTo(post.getPostImageUri())
            .jsonPath("$.location.latitude").isEqualTo(post.getLocation().getX())
            .jsonPath("$.location.longitude").isEqualTo(post.getLocation().getY());

// Verify whether the post has properly been stored in db / cache 
}

我的問題是是否有辦法:

  • 提供一個回調,使我可以整體獲取json結果,以驗證對象是否已正確存儲在數據庫和緩存中;
  • 阻止此方法,以便以后可以驗證結果。

以下似乎有效:

    FluxExchangeResult<Post> res = webTestClient.post().uri(BASE_URI)
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .body(Mono.just(post), Post.class)
            .exchange()
            .returnResult(Post.class);

    Post retPost = res.getResponseBody().blockFirst();

這意味着以后我將不得不手動驗證結果。
如果您有更好的選擇,仍然歡迎您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM