类 MockRestServiceServer
- java.lang.Object
- org.springframework.test.web.client.MockRestServiceServer
public final 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 theRestTemplateas 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
MockMvcClientHttpRequestFactoryon aRestTemplatewhich allows executing requests against an instance ofMockMvc.- 从以下版本开始:
- 3.2
- 作者:
- Craig Walls, Rossen Stoyanchev
嵌套类概要
嵌套类 修饰符和类型 类 说明 static interfaceMockRestServiceServer.MockRestServiceServerBuilderBuilder to create aMockRestServiceServer.
方法概要
所有方法 静态方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 static MockRestServiceServer.MockRestServiceServerBuilderbindTo(AsyncRestTemplate asyncRestTemplate)已过时。see deprecation notice onAsyncRestTemplateitselfstatic MockRestServiceServer.MockRestServiceServerBuilderbindTo(RestTemplate restTemplate)Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestTemplate.static MockRestServiceServer.MockRestServiceServerBuilderbindTo(RestGatewaySupport restGatewaySupport)Return a builder for aMockRestServiceServerthat should be used to reply to the givenRestGatewaySupport.static MockRestServiceServercreateServer(AsyncRestTemplate asyncRestTemplate)已过时。see deprecation notice onAsyncRestTemplateitselfstatic MockRestServiceServercreateServer(RestTemplate restTemplate)A shortcut forbindTo(restTemplate).build().static MockRestServiceServercreateServer(RestGatewaySupport restGateway)A shortcut forbindTo(restGateway).build().ResponseActionsexpect(ExpectedCount count, RequestMatcher matcher)An alternative toexpect(RequestMatcher)that also indicates how many times the request is expected to be executed.ResponseActionsexpect(RequestMatcher matcher)Set up an expectation for a single HTTP request.voidreset()Reset the internal state removing all expectations and recorded requests.voidverify()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 returnedResponseActionscan 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
RestTemplatein 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 aMockRestServiceServerthat should be used to reply to the givenRestTemplate.- 从以下版本开始:
- 4.3
bindTo
@Deprecated public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(AsyncRestTemplate asyncRestTemplate)
已过时。see deprecation notice onAsyncRestTemplateitselfReturn a builder for aMockRestServiceServerthat should be used to reply to the givenAsyncRestTemplate.- 从以下版本开始:
- 4.3
bindTo
public static MockRestServiceServer.MockRestServiceServerBuilder bindTo(RestGatewaySupport restGatewaySupport)
Return a builder for aMockRestServiceServerthat 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
@Deprecated public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate)
已过时。see deprecation notice onAsyncRestTemplateitselfA 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