How to test webclient of Disposable type call in Spring Webflux

Viewed 32

Is it possible to use stubFor here? If yes, how to do it? If not, how to test this code?

    public Mono<Void> trelloCallback(Type payload) {
        webClient.post()
                 .uri(URL)
                 .bodyValue(payload)
                 .headers(httpHeaders -> httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                 .retrieve()
                 .bodyToMono(Void.class)
                 .subscribe();
        return Mono.empty();
    }

Info: If we add .subscribe to the code, it runs in the background.

1 Answers

I test the result, and I can see WireMock.stubFor is working with the subscribe call.

For more explanation, We first create a WireMock.stubFor for some endpoint call. Then we make an actual call using the webTestClient to do integration test. And then verify the result using

verify(exactly(1), postRequestedFor(urlEqualTo(SOME_URL)));
// For SOME_URL create a `stubFor`
Related