简体   繁体   English

春季启动WebTestClient全面集成测试

[英]spring boot WebTestClient full integration test

I want to use WebTestClient for performing a full integration test. 我想使用WebTestClient执行完整的集成测试。
My test is simple: 我的测试很简单:

@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 
}

My question is whether there is a way to either: 我的问题是是否有办法:

  • Provide a callback where I can get the json result as a whole to verify whether the object has correctly been stored in my database and cache; 提供一个回调,使我可以整体获取json结果,以验证对象是否已正确存储在数据库和缓存中;
  • Make this method blocking so that I can verify the result afterwards. 阻止此方法,以便以后可以验证结果。

The following seems to work: 以下似乎有效:

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

This means that I will have to manually verify the result afterwards. 这意味着以后我将不得不手动验证结果。
If you have a better alternative, this is still welcome. 如果您有更好的选择,仍然欢迎您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM