Skip to content

Commit 059337f

Browse files
committed
Merge pull request #15978 from nosan
* pr/15978: Polish contribution Add an example showing how to use Spring REST Docs with WebTestClient
2 parents 38a20f2 + ce17a92 commit 059337f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7695,6 +7695,28 @@ generate the default snippets. The following example shows a
76957695

76967696

76977697

7698+
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-web-test-client]]
7699+
===== Auto-configured Spring REST Docs Tests with WebTestClient
7700+
`@AutoConfigureRestDocs` can also be used with `WebTestClient`. You can inject it by using
7701+
`@Autowired` and use it in your tests as you normally would when using `@WebFluxTest` and
7702+
Spring REST Docs, as shown in the following example:
7703+
7704+
[source,java,indent=0]
7705+
----
7706+
include::{code-examples}/test/autoconfigure/restdocs/webclient/UsersDocumentationTests.java[tag=source]
7707+
----
7708+
7709+
If you require more control over Spring REST Docs configuration than offered by the
7710+
attributes of `@AutoConfigureRestDocs`, you can use a
7711+
`RestDocsWebTestClientConfigurationCustomizer` bean, as shown in the following example:
7712+
7713+
[source,java,indent=0]
7714+
----
7715+
include::{code-examples}/test/autoconfigure/restdocs/webclient/AdvancedConfigurationExample.java[tag=configuration]
7716+
----
7717+
7718+
7719+
76987720
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured]]
76997721
===== Auto-configured Spring REST Docs Tests with REST Assured
77007722
`@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.test.autoconfigure.restdocs.webclient;
18+
19+
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsWebTestClientConfigurationCustomizer;
20+
import org.springframework.boot.test.context.TestConfiguration;
21+
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
22+
23+
public class AdvancedConfigurationExample {
24+
25+
// tag::configuration[]
26+
@TestConfiguration
27+
public static class CustomizationConfiguration
28+
implements RestDocsWebTestClientConfigurationCustomizer {
29+
30+
@Override
31+
public void customize(WebTestClientRestDocumentationConfigurer configurer) {
32+
configurer.snippets().withEncoding("UTF-8");
33+
}
34+
35+
}
36+
// end::configuration[]
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.test.autoconfigure.restdocs.webclient;
18+
19+
// tag::source[]
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
25+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
26+
import org.springframework.test.context.junit4.SpringRunner;
27+
import org.springframework.test.web.reactive.server.WebTestClient;
28+
29+
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;
30+
31+
@RunWith(SpringRunner.class)
32+
@WebFluxTest
33+
@AutoConfigureRestDocs
34+
public class UsersDocumentationTests {
35+
36+
@Autowired
37+
private WebTestClient webTestClient;
38+
39+
@Test
40+
void listUsers() {
41+
this.webTestClient.get().uri("/").exchange().expectStatus().isOk().expectBody()
42+
.consumeWith(document("list-users"));
43+
}
44+
45+
}
46+
// end::source[]

0 commit comments

Comments
 (0)