Hi everyone. What could be a way to test a service...
# spring
t
Hi everyone. What could be a way to test a service that uses
WebClient
to perform requests ? Mocking
WebClient
does not seem to be a viable solution. I've seen something like
WebClientTest
on the web, but I'm unable to find the correct dependency or documentation about it.
My bad, its
WebTestClient
...
c
you could do wiremock
k
for me webclient is just tool, you should wrap in it some kind of DAO and mock that, or as @Czar mention, mock the service you are calling
t
That's the layer I'd like to unit test : to check that the service implementation parses JSON correctly and handles HTTP errors.
j
Not sure if I understand correctly, but if you want your test to use actual implementation of WebClient, then use WireMock/MockWebServer to mock "server side". If it's WebClient you want to mock then use Mockito for example to mock that instance?
t
I'm looking for something similar to Ktor's
MockEngine
to simulate a remote server without issuing actual requests. Something I could inject into or in place of
WebClient
to configure fake server responses.
j
Then WireMock is what you are looking for, so WebClient makes requests to WireMock instead of real endpoint
t
MockWebServer is easy to set up and use from your test: https://github.com/square/okhttp/tree/master/mockwebserver
b
I have used MockWebServer before too and it works great.