类 MockRestServiceServer
- java.lang.Object
- org.springframework.test.web.client.MockRestServiceServer
public class MockRestServiceServer extends Object
Main entry point for client-side REST testing. Used for tests that involve direct or indirect use of theRestTemplate
. Provides a way to set up expected requests that will be performed through theRestTemplate
as well as mock responses to send back thus removing the need for an actual server.Below is an example that assumes static imports from
MockRestRequestMatchers
,MockRestResponseCreators
, andExpectedCount
:RestTemplate restTemplate = new RestTemplate() MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build(); server.expect(manyTimes(), requestTo("/hotels/42")).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON)); Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42); // Use the hotel instance... // Verify all expectations met server.verify();
Note that as an alternative to the above you can also set the
MockMvcClientHttpRequestFactory
on aRestTemplate
which allows executing requests against an instance ofMockMvc
.- 从以下版本开始:
- 3.2
- 作者:
- Craig Walls, Rossen Stoyanchev
嵌套类概要
嵌套类 修饰符和类型 类 说明 static interface
MockRestServiceServer.MockRestServiceServerBuilder
Builder to create aMockRestServiceServer
.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static MockRestServiceServer.MockRestServiceServerBuilder
bindTo(AsyncRestTemplate asyncRestTemplate)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenAsyncRestTemplate
.static MockRestServiceServer.MockRestServiceServerBuilder
bindTo(RestTemplate restTemplate)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenRestTemplate
.static MockRestServiceServer.MockRestServiceServerBuilder
bindTo(RestGatewaySupport restGateway)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenRestGatewaySupport
.static MockRestServiceServer
createServer(AsyncRestTemplate asyncRestTemplate)
A shortcut forbindTo(asyncRestTemplate).build()
.static MockRestServiceServer
createServer(RestTemplate restTemplate)
A shortcut forbindTo(restTemplate).build()
.static MockRestServiceServer
createServer(RestGatewaySupport restGateway)
A shortcut forbindTo(restGateway).build()
.ResponseActions
expect(ExpectedCount count, RequestMatcher matcher)
An alternative toexpect(RequestMatcher)
that also indicates how many times the request is expected to be executed.ResponseActions
expect(RequestMatcher matcher)
Set up an expectation for a single HTTP request.void
reset()
Reset the internal state removing all expectations and recorded requests.void
verify()
Verify that all expected requests set up viaexpect(RequestMatcher)
were indeed performed.
方法详细资料
expect
public ResponseActions expect(RequestMatcher matcher)
Set up an expectation for a single HTTP request. The returnedResponseActions
can be used to set up further expectations as well as to define the response.This method may be invoked any number times before starting to make request through the underlying
RestTemplate
in order to set up all expected requests.- 参数:
matcher
- request matcher- 返回:
- a representation of the expectation
expect
public ResponseActions expect(ExpectedCount count, RequestMatcher matcher)
An alternative toexpect(RequestMatcher)
that also indicates how many times the request is expected to be executed.When request expectations have an expected count greater than one, only the first execution is expected to match the order of declaration. Subsequent request executions may be inserted anywhere thereafter.
- 参数:
count
- the expected countmatcher
- request matcher- 返回:
- a representation of the expectation
- 从以下版本开始:
- 4.3
verify
public void verify()
Verify that all expected requests set up viaexpect(RequestMatcher)
were indeed performed.- 抛出:
AssertionError
- when some expectations were not met
reset
public void reset()
Reset the internal state removing all expectations and recorded requests.
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestTemplate restTemplate)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenRestTemplate
.- 从以下版本开始:
- 4.3
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(AsyncRestTemplate asyncRestTemplate)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenAsyncRestTemplate
.- 从以下版本开始:
- 4.3
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestGatewaySupport restGateway)
Return a builder for aMockRestServiceServer
that should be used to reply to the givenRestGatewaySupport
.- 从以下版本开始:
- 4.3
createServer
public static MockRestServiceServer createServer(RestTemplate restTemplate)
A shortcut forbindTo(restTemplate).build()
.- 参数:
restTemplate
- the RestTemplate to set up for mock testing- 返回:
- the mock server
createServer
public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate)
A shortcut forbindTo(asyncRestTemplate).build()
.- 参数:
asyncRestTemplate
- the AsyncRestTemplate to set up for mock testing- 返回:
- the created mock server
createServer
public static MockRestServiceServer createServer(RestGatewaySupport restGateway)
A shortcut forbindTo(restGateway).build()
.- 参数:
restGateway
- the REST gateway to set up for mock testing- 返回:
- the created mock server