【问题标题】:spring boot WebTestClient full integration testspring boot WebTestClient全集成测试
【发布时间】:2018-12-09 14:47:06
【问题描述】:

我想使用 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 结果,以验证对象是否已正确存储在我的数据库和缓存中;
  • 将此方法设为阻塞,以便之后验证结果。

【问题讨论】:

    标签: unit-testing spring-boot junit


    【解决方案1】:

    以下似乎有效:

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

    这意味着我必须在之后手动验证结果。
    如果你有更好的选择,这仍然是受欢迎的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2020-04-24
      • 2017-10-27
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多