From 027e6baba53a83e385bc307925c654c55da346a8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 10 Jan 2018 16:15:15 +0100 Subject: [PATCH] Improve documentation for WebTestClient Closes gh-11203 --- .../main/asciidoc/spring-boot-features.adoc | 15 ++++-- ...ndomPortTestRestTemplateExampleTests.java} | 2 +- .../RandomPortWebTestClientExampleTests.java | 47 +++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) rename spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/{RandomPortExampleTests.java => RandomPortTestRestTemplateExampleTests.java} (96%) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortWebTestClientExampleTests.java diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 238c36f8e0..4c4bd500d0 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5907,9 +5907,16 @@ running server, as shown in the following example: [source,java,indent=0] ---- -include::{code-examples}/test/web/RandomPortExampleTests.java[tag=test-random-port] +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] +---- +include::{code-examples}/test/web/RandomPortWebTestClientExampleTests.java[tag=test-random-port] +---- [[boot-features-testing-spring-boot-applications-mocking-beans]] @@ -6194,8 +6201,10 @@ additional configuration classes using `@Import` on your test. Often, `@WebFluxTest` is limited to a single controller and used in combination with the `@MockBean` annotation to provide mock implementations for required collaborators. -`@WebFluxTest` also auto-configures `WebTestClient`, which offers a powerful way to -quickly test WebFlux controllers without needing to start a full HTTP server. +`@WebFluxTest` also auto-configures +{spring-reference}testing.html#webtestclient[`WebTestClient`]`WebTestClient`, which offers +a powerful way to quickly test WebFlux controllers without needing to start a full HTTP +server. TIP: You can also auto-configure `WebTestClient` in a non-`@WebFluxTest` (such as `@SpringBootTest`) by annotating it with `@AutoConfigureWebTestClient`. The following diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortExampleTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortTestRestTemplateExampleTests.java similarity index 96% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortExampleTests.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortTestRestTemplateExampleTests.java index 8bba7f0590..e7d361e0de 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortExampleTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortTestRestTemplateExampleTests.java @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -public class RandomPortExampleTests { +public class RandomPortTestRestTemplateExampleTests { @Autowired private TestRestTemplate restTemplate; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortWebTestClientExampleTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortWebTestClientExampleTests.java new file mode 100644 index 0000000000..fcab942506 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/test/web/RandomPortWebTestClientExampleTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.web; + +// tag::test-random-port[] + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class RandomPortWebTestClientExampleTests { + + @Autowired + private WebTestClient webClient; + + @Test + public void exampleTest() { + this.webClient.get() + .uri("/") + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("Hello World"); + } + +} +// tag::test-random-port[]