0

I have an @Service class that does some null checks and then makes a call to an external micro-service using WebClient. Sonar is complaining that this class is not test because the method is not fully tested. Question is, how can I mock this call or use a mockserver for this? I've tried WebTestClient but I can't seem to get things to work..

public Mono<CartResponse> someServiceCall(BarRequest barRequest)

//some null checks and random logic.

return WebClient.create("http://" + fooHostName)
                .post()
                .uri(uri)
                .body(Mono.just(barRequest), BarRequest.class)
                .exchange()
                .flatMap(serviceResponse -> {
                    if (serviceResponse.statusCode().is5xxServerError()) {

                        //some error logic
                        return Mono.just(barResponse);
                    }
                    return serviceResponse.bodyToMono(BarResponse.class);
                });

So I don't want to actually make this call I just want it covered in the test, so I'd like some insight on how to get this either mocked or spin up a mock server.. I've been at this for about a day now..

Sierra Bravo
  • 41
  • 2
  • 8

1 Answers1

1

this is as of yet no supported for mocking WebClient like RestTemplate.

there is an open issue for it on github.

Support of MockRestServiceServer for WebClient

Spring themselves use MockWebServer to test their own code so it's safe to say that it is a viable solution.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54