Larry Garfield
07/17/2024, 9:23 PMJacob
07/17/2024, 11:41 PMJacob
07/17/2024, 11:41 PMJacob
07/18/2024, 12:43 AMthanksforallthefish
07/18/2024, 6:31 AMorg.springframework.web.client.RestClient
since RestTemplate
is kept around in maintenance mode only because it's used a lot, but it has a lot of limitations (like inability to stream), but we had a bunch of test using MockRestServiceServer
bound to a RestTemplate
before and did not have any problemLarry Garfield
07/18/2024, 1:17 PMJacob
07/18/2024, 1:20 PMLarry Garfield
07/18/2024, 1:30 PMLarry Garfield
07/18/2024, 2:09 PMandExpect().andRespond()
on it. Neither of which methods appear to be defined.thanksforallthefish
07/18/2024, 2:22 PMFYI spring never implemented any similar testing tool for webclient. They recommended just using a mock server librarybut they extended
MockRestServiceServer
to support the even newer RestClient
, so I guess they are still making up their minds, probably based on community feedback. A mock server library, like Wiremock
or okhttp3.mockwebserver
would also be an option and it would transparent for future changes (should you remove the RestTemplate
at some point you would not need to change anything in the test). I find them more cumbersome to use though, but it's probably just a matter of habitthanksforallthefish
07/18/2024, 2:24 PMandExpect
is not a method on the RequestMatcher
, eg:
import org.springframework.test.web.client.match.MockRestRequestMatchers.method
import org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo
import org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess
import org.springframework.http.HttpMethod
server.expect(
requestTo("...")
)
.andExpect(
method(HttpMethod.POST)
)
.andRespond(withSuccess())
Jacob
07/18/2024, 10:37 PMHow does that even work? You stick a fake server behind the real rest client somehow? That seems… complicated.
You add a list of canned responses to the server. The server saves every request it gets. You look at the list of requests and assert that they look right.