简体   繁体   中英

spring boot WebTestClient full integration test

I want to use WebTestClient for performing a full integration test.
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;
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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