Skip to content

Commit cf89ebc

Browse files
nosansnicoll
authored andcommitted
Add an example showing how to use Spring REST Docs with WebTestClient
See gh-15978
1 parent 38a20f2 commit cf89ebc

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7716,7 +7716,25 @@ bean can be used, as shown in the following example:
77167716
include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java[tag=configuration]
77177717
----
77187718

7719+
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-web-test-client]]
7720+
===== Auto-configured Spring REST Docs Tests with WebTestClient
7721+
`@AutoConfigureRestDocs` could be used in conjunction with a `@WebFluxTest` to generate REST Docs.
7722+
`@AutoConfigureRestDocs` customizes the `WebTestClient` bean to use Spring REST Docs. You can
7723+
inject it by using `@Autowired` and use it in your tests. Here is a quick sample:
77197724

7725+
[source,java,indent=0]
7726+
----
7727+
include::{code-examples}/test/autoconfigure/restdocs/webclient/UsersDocumentationTests.java[tag=source]
7728+
----
7729+
7730+
If you require more control over Spring REST Docs configuration than offered by the
7731+
attributes of `@AutoConfigureRestDocs`, you can use a
7732+
`RestDocsWebTestClientConfigurationCustomizer` bean, as shown in the following example:
7733+
7734+
[source,java,indent=0]
7735+
----
7736+
include::{code-examples}/test/autoconfigure/restdocs/webclient/AdvancedRestDocsWebTestClientConfigurationExample.java[tag=configuration]
7737+
----
77207738

77217739
[[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]]
77227740
==== Additional Auto-configuration and Slicing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
package org.springframework.boot.docs.test.autoconfigure.restdocs.webclient;
17+
18+
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsWebTestClientConfigurationCustomizer;
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
21+
22+
public class AdvancedRestDocsWebTestClientConfigurationExample {
23+
24+
// tag::configuration[]
25+
@TestConfiguration
26+
static class CustomizationConfiguration
27+
implements RestDocsWebTestClientConfigurationCustomizer {
28+
29+
@Override
30+
public void customize(WebTestClientRestDocumentationConfigurer configurer) {
31+
configurer.snippets().withEncoding("UTF-8");
32+
}
33+
34+
}
35+
// end::configuration[]
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.springframework.boot.docs.test.autoconfigure.restdocs.webclient;
17+
18+
// tag::source[]
19+
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+
}
47+
// end::source[]

0 commit comments

Comments
 (0)