Improve reference doc for WebTestClient

Closes gh-11203
This commit is contained in:
Brian Clozel 2018-02-14 15:39:48 +01:00
parent 17258eeea3
commit 1f8d489e5a
1 changed files with 30 additions and 14 deletions

View File

@ -6093,31 +6093,32 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
[[boot-features-testing-spring-boot-applications-working-with-random-ports]] [[boot-features-testing-spring-boot-applications-testing-with-running-server]]
==== Working with Random Ports ==== Testing with a running server
If you need to start a full running server for tests, we recommend that you use random If you need to start a full running server, we recommend that you use random ports.
ports. If you use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)`, an If you use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)`, an
available port is picked at random each time your test runs. available port is picked at random each time your test runs.
The `@LocalServerPort` annotation can be used to The `@LocalServerPort` annotation can be used to
<<howto-discover-the-http-port-at-runtime,inject the actual port used>> into your test. <<howto-discover-the-http-port-at-runtime,inject the actual port used>> into your test.
For convenience, tests that need to make REST calls to the started server can For convenience, tests that need to make REST calls to the started server can
additionally `@Autowire` a `TestRestTemplate`, which resolves relative links to the additionally `@Autowire` a
running server, as shown in the following example: {spring-reference}testing.html#webtestclient-tests[`WebTestClient`], which resolves
relative links to the running server and comes with a dedicated API for verifying
[source,java,indent=0] responses, as shown in the following example:
----
include::{code-examples}/test/web/RandomPortTestRestTemplateExampleTests.java[tag=test-random-port]
----
If you prefer to use a {spring-reference}testing.html#webtestclient[`WebTestClient`], you
can use that as well:
[source,java,indent=0] [source,java,indent=0]
---- ----
include::{code-examples}/test/web/RandomPortWebTestClientExampleTests.java[tag=test-random-port] include::{code-examples}/test/web/RandomPortWebTestClientExampleTests.java[tag=test-random-port]
---- ----
Spring Boot also provides a `TestRestTemplate` facility:
[source,java,indent=0]
----
include::{code-examples}/test/web/RandomPortTestRestTemplateExampleTests.java[tag=test-random-port]
----
[[boot-features-testing-spring-boot-applications-mocking-beans]] [[boot-features-testing-spring-boot-applications-mocking-beans]]
==== Mocking and Spying Beans ==== Mocking and Spying Beans
@ -6389,6 +6390,9 @@ definition.
A list of the auto-configuration settings that are enabled by `@WebMvcTest` can be A list of the auto-configuration settings that are enabled by `@WebMvcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>. <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
TIP: Sometimes writing Spring MVC tests is not enough; Spring Boot can help you running
<<boot-features-testing-spring-boot-applications-testing-with-running-server,
full end-to-end tests with an actual server>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]]
@ -6452,6 +6456,9 @@ example shows a class that uses both `@WebFluxTest` and a `WebTestClient`:
A list of the auto-configuration that is enabled by `@WebFluxTest` can be A list of the auto-configuration that is enabled by `@WebFluxTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>. <<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
TIP: Sometimes writing Spring MVC tests is not enough; Spring Boot can help you running
<<boot-features-testing-spring-boot-applications-testing-with-running-server,
full end-to-end tests with an actual server>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]] [[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
@ -7110,6 +7117,15 @@ for assertions, as follows:
[[boot-features-rest-templates-test-utility]] [[boot-features-rest-templates-test-utility]]
==== TestRestTemplate ==== TestRestTemplate
TIP: Spring Framework 5.0 provides a new `WebTestClient` that works for
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests,
WebFlux integration tests>> and both
<<boot-features-testing-spring-boot-applications-testing-with-running-server,
WebFlux and MVC end-to-end testing>>. It provides a fluent API for assertions,
unlike `TestRestTemplate`.
`TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful `TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
in integration tests. You can get a vanilla template or one that sends Basic HTTP in integration tests. You can get a vanilla template or one that sends Basic HTTP
authentication (with a username and password). In either case, the template behaves in a authentication (with a username and password). In either case, the template behaves in a