提交 4818378c 编写于 作者: R Rossen Stoyanchev

Better document client-side tests against MockMvc

Issue: SPR-13815
上级 7690f27c
...@@ -80,6 +80,12 @@ import org.springframework.web.client.support.RestGatewaySupport; ...@@ -80,6 +80,12 @@ import org.springframework.web.client.support.RestGatewaySupport;
* classes), you can typically use the Code Completion features (i.e. * classes), you can typically use the Code Completion features (i.e.
* ctrl-space) in your IDE to set up the mocks. * ctrl-space) in your IDE to set up the mocks.
* *
* <p>An alternative to the above is to use
* {@link MockMvcClientHttpRequestFactory} which allows executing requests
* against a {@link org.springframework.test.web.servlet.MockMvc MockMvc}
* instance. That allows you to process requests using your server-side code
* but without running a server.
*
* <p><strong>Credits:</strong> The client-side REST testing support was * <p><strong>Credits:</strong> The client-side REST testing support was
* inspired by and initially based on similar code in the Spring WS project for * inspired by and initially based on similar code in the Spring WS project for
* client-side tests involving the {@code WebServiceTemplate}. * client-side tests involving the {@code WebServiceTemplate}.
......
...@@ -5035,8 +5035,10 @@ http://www.gebish.org/manual/current/[The Book of Geb] user's manual. ...@@ -5035,8 +5035,10 @@ http://www.gebish.org/manual/current/[The Book of Geb] user's manual.
[[spring-mvc-test-client]] [[spring-mvc-test-client]]
==== Client-Side REST Tests ==== Client-Side REST Tests
Client-side tests are for code using the `RestTemplate`. The goal is to define expected Client-side tests can be used to test code that internally uses the `RestTemplate`.
requests and provide "stub" responses: The idea is to declare expected requests and to provide "stub" responses so that
you can focus on testing the code in isolation, i.e. without running a server.
Here is an example:
[source,java,indent=0] [source,java,indent=0]
[subs="verbatim,quotes"] [subs="verbatim,quotes"]
...@@ -5046,7 +5048,7 @@ requests and provide "stub" responses: ...@@ -5046,7 +5048,7 @@ requests and provide "stub" responses:
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(requestTo("/greeting")).andRespond(withSuccess("Hello world", MediaType.TEXT_PLAIN)); mockServer.expect(requestTo("/greeting")).andRespond(withSuccess("Hello world", MediaType.TEXT_PLAIN));
// use RestTemplate ... // Test code that uses the above RestTemplate ...
mockServer.verify(); mockServer.verify();
---- ----
...@@ -5056,12 +5058,28 @@ tests -- configures the `RestTemplate` with a custom `ClientHttpRequestFactory` ...@@ -5056,12 +5058,28 @@ tests -- configures the `RestTemplate` with a custom `ClientHttpRequestFactory`
asserts actual requests against expectations and returns "stub" responses. In this case asserts actual requests against expectations and returns "stub" responses. In this case
we expect a single request to "/greeting" and want to return a 200 response with we expect a single request to "/greeting" and want to return a 200 response with
"text/plain" content. We could define as many additional requests and stub responses as "text/plain" content. We could define as many additional requests and stub responses as
necessary. necessary. Once expected requests and stub responses have been defined, the `RestTemplate` can be
Once expected requests and stub responses have been defined, the `RestTemplate` can be
used in client-side code as usual. At the end of the tests `mockServer.verify()` can be used in client-side code as usual. At the end of the tests `mockServer.verify()` can be
used to verify that all expected requests were performed. used to verify that all expected requests were performed.
The client-side test support also provides an alternative `ClientHttpRequestFactory`
strategy for executing requests with a `MockMvc` instance. That allows you to
process requests using your server-side code but without running a server.
Here is an example:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
this.restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
// Test code that uses the above RestTemplate ...
mockServer.verify();
----
[[spring-mvc-test-client-static-imports]] [[spring-mvc-test-client-static-imports]]
===== Static Imports ===== Static Imports
Just like with server-side tests, the fluent API for client-side tests requires a few Just like with server-side tests, the fluent API for client-side tests requires a few
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册