Improve documentation for WebTestClient
Closes gh-11203
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
@@ -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[]
|
||||
Reference in New Issue
Block a user