You can use client-side tests to test code that internally uses RestTemplate
. The idea is to declare
expected requests and provide "stub responses" so that you can focus on testing your code in an isolated environment
(that is, without running the server). The following example shows how to do this:
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
mockServer.expect(requestTo("/greeting")).andRespond(withSuccess());
// Test code using the above RestTemplate...
mockServer.verify();
val restTemplate = RestTemplate()
val mockServer = MockRestServiceServer.bindTo(restTemplate).build()
mockServer.expect(requestTo("/greeting")).andRespond(withSuccess())
// Test code using the above RestTemplate...
mockServer.verify()
In the previous example, MockRestServiceServer
(the central class for client-side REST tests) configures
RestTemplate
using a special ClientHttpRequestFactory
that validates that actual requests
match expected events and returns "stub responses". In this case, we are expecting a request for
/greeting
and want to return a 200 response with the content text/plain
. If necessary, you
can define additional expected requests and stub responses. If we define expected stub requests and responses,
RestTemplate
can be used in client-side code as usual. At the end of testing, you can use mockServer.verify()
to ensure that all expected events have occurred.
By default, requests are expected in the order in which the expected events were declared. You can set the ignoreExpectOrder
option when creating the server, in which case all expected events will be checked (in order) to find a match for
the specified request. This means that requests can come in any order. The following example uses ignoreExpectOrder
:
server = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
server = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build()
Even with unordered queries, by default each query is only allowed to be executed once. The expect
method provides an overload that takes an ExpectedCount
argument that specifies the range of the count
(for example, once
, manyTimes
, max
, min
, between
and so on). The following example uses the range times
:
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
mockServer.expect(times(2), requestTo("/something")).andRespond(withSuccess());
mockServer.expect(times(3), requestTo("/somewhere")).andRespond(withSuccess());
// ...
mockServer.verify();
val restTemplate = RestTemplate()
val mockServer = MockRestServiceServer.bindTo(restTemplate).build()
mockServer.expect(times(2), requestTo("/something")).andRespond(withSuccess())
mockServer.expect(times(3), requestTo("/somewhere")).andRespond(withSuccess())
// ...
mockServer.verify()
Note that if ignoreExpectOrder
is not set (the default), and therefore requests are expected in
declaration order, then that order only applies to the first of the requests expected. For example, if "/something"
is expected twice, and then "/somewhere" three times, then the request to "/something" must come before the request
to "/somewhere", but in addition to these subsequent "/something" and "/somewhere ", requests can arrive at any
time.
As an alternative to all of the above, the client-side testing support feature also provides a ClientHttpRequestFactory
implementation that can be configured in the RestTemplate
to bind to an instance of
MockMvc
. This allows you to process requests using real server-side logic, but without running the
server. The following example shows how to do this:
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
this.restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
// Test code using the above RestTemplate ...
val mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build()
restTemplate = RestTemplate(MockMvcClientHttpRequestFactory(mockMvc))
// Test code using the above RestTemplate ...
Static import
As with server-side tests, a fluid API for client-side tests requires some static elements to be imported. They can
be easily found by searching MockRest*
. Eclipse users need to add
MockRestRequestMatchers.*
and MockRestResponseCreators.*
as "favorite static members" in
Eclipse preferences under Java → Editor → Content Assist → Favorites. This will allow you to use content assist
after entering the first character of the static method name. Other IDEs (such as IntelliJ) may not require
additional configuration. Check code completion support for static members.
Other examples of client-side REST tests
Native Spring MVC Tests include test examples REST on the client side.