Skip to content

Commit 5d09260

Browse files
committed
Reinstate support for REST Assured
Closes gh-29543 Closes gh-30426
1 parent c7bf816 commit 5d09260

File tree

15 files changed

+499
-5
lines changed

15 files changed

+499
-5
lines changed

spring-boot-project/spring-boot-dependencies/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,19 @@ bom {
11411141
]
11421142
}
11431143
}
1144+
library("REST Assured", "5.0.0") {
1145+
group("io.rest-assured") {
1146+
modules = [
1147+
"json-path",
1148+
"json-schema-validator",
1149+
"rest-assured",
1150+
"scala-support",
1151+
"spring-mock-mvc",
1152+
"spring-web-test-client",
1153+
"xml-path"
1154+
]
1155+
}
1156+
}
11441157
library("RSocket", "1.1.1") {
11451158
group("io.rsocket") {
11461159
imports = [

spring-boot-project/spring-boot-docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ dependencies {
152152
implementation("org.springframework.restdocs:spring-restdocs-mockmvc") {
153153
exclude group: "javax.servlet", module: "javax.servlet-api"
154154
}
155+
implementation("org.springframework.restdocs:spring-restdocs-restassured")
155156
implementation("org.springframework.restdocs:spring-restdocs-webtestclient")
156157
implementation("org.springframework.security:spring-security-config")
157158
implementation("org.springframework.security:spring-security-oauth2-client")

spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-clien
349349
boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs=features.testing.spring-boot-applications.autoconfigured-spring-restdocs
350350
boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-mock-mvc=features.testing.spring-boot-applications.autoconfigured-spring-restdocs.with-mock-mvc
351351
boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-web-test-client=features.testing.spring-boot-applications.autoconfigured-spring-restdocs.with-web-test-client
352+
boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured=features.testing.spring-boot-applications.autoconfigured-spring-restdocs.with-rest-assured
352353
boot-features-testing-spring-boot-applications-testing-autoconfigured-webservices=features.testing.spring-boot-applications.autoconfigured-webservices
353354
boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config=features.testing.spring-boot-applications.additional-autoconfiguration-and-slicing
354355
boot-features-testing-spring-boot-applications-testing-user-configuration=features.testing.spring-boot-applications.user-configuration-and-slicing

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ include::code:MyRestClientTests[]
646646

647647
[[features.testing.spring-boot-applications.autoconfigured-spring-restdocs]]
648648
==== Auto-configured Spring REST Docs Tests
649-
You can use the `@AutoConfigureRestDocs` annotation to use {spring-restdocs}[Spring REST Docs] in your tests with Mock MVC or WebTestClient.
649+
You can use the `@AutoConfigureRestDocs` annotation to use {spring-restdocs}[Spring REST Docs] in your tests with Mock MVC, REST Assured, or WebTestClient.
650650
It removes the need for the JUnit extension in Spring REST Docs.
651651

652652
`@AutoConfigureRestDocs` can be used to override the default output directory (`target/generated-snippets` if you are using Maven or `build/generated-snippets` if you are using Gradle).
@@ -691,6 +691,19 @@ include::code:MyWebTestClientBuilderCustomizerConfiguration[]
691691

692692

693693

694+
[[features.testing.spring-boot-applications.autoconfigured-spring-restdocs.with-rest-assured]]
695+
===== Auto-configured Spring REST Docs Tests with REST Assured
696+
`@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring REST Docs, available to your tests.
697+
You can inject it by using `@Autowired` and use it in your tests as you normally would when using REST Assured and Spring REST Docs, as shown in the following example:
698+
699+
include::code:MyUserDocumentationTests[]
700+
701+
If you require more control over Spring REST Docs configuration than offered by the attributes of `@AutoConfigureRestDocs`, a `RestDocsRestAssuredConfigurationCustomizer` bean can be used, as shown in the following example:
702+
703+
include::code:MyRestDocsConfiguration[]
704+
705+
706+
694707
[[features.testing.spring-boot-applications.autoconfigured-webservices]]
695708
==== Auto-configured Spring Web Services Tests
696709

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.features.testing.springbootapplications.autoconfiguredspringrestdocs.withrestassured;
18+
19+
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer;
20+
import org.springframework.boot.test.context.TestConfiguration;
21+
import org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer;
22+
import org.springframework.restdocs.templates.TemplateFormats;
23+
24+
@TestConfiguration(proxyBeanMethods = false)
25+
public class MyRestDocsConfiguration implements RestDocsRestAssuredConfigurationCustomizer {
26+
27+
@Override
28+
public void customize(RestAssuredRestDocumentationConfigurer configurer) {
29+
configurer.snippets().withTemplateFormat(TemplateFormats.markdown());
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.features.testing.springbootapplications.autoconfiguredspringrestdocs.withrestassured;
18+
19+
import io.restassured.specification.RequestSpecification;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
26+
import org.springframework.boot.web.server.LocalServerPort;
27+
28+
import static io.restassured.RestAssured.given;
29+
import static org.hamcrest.Matchers.is;
30+
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document;
31+
32+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
33+
@AutoConfigureRestDocs
34+
class MyUserDocumentationTests {
35+
36+
@Test
37+
void listUsers(@Autowired RequestSpecification documentationSpec, @LocalServerPort int port) {
38+
// @formatter:off
39+
given(documentationSpec)
40+
.filter(document("list-users"))
41+
.when()
42+
.port(port)
43+
.get("/")
44+
.then().assertThat()
45+
.statusCode(is(200));
46+
// @formatter:on
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.features.testing.springbootapplications.autoconfiguredspringrestdocs.withrestassured
18+
19+
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer
20+
import org.springframework.boot.test.context.TestConfiguration
21+
import org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer
22+
import org.springframework.restdocs.templates.TemplateFormats
23+
24+
@TestConfiguration(proxyBeanMethods = false)
25+
class MyRestDocsConfiguration : RestDocsRestAssuredConfigurationCustomizer {
26+
27+
override fun customize(configurer: RestAssuredRestDocumentationConfigurer) {
28+
configurer.snippets().withTemplateFormat(TemplateFormats.markdown())
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.features.testing.springbootapplications.autoconfiguredspringrestdocs.withrestassured
18+
19+
import io.restassured.RestAssured
20+
import io.restassured.specification.RequestSpecification
21+
import org.hamcrest.Matchers
22+
import org.junit.jupiter.api.Test
23+
import org.springframework.beans.factory.annotation.Autowired
24+
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
25+
import org.springframework.boot.test.context.SpringBootTest
26+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
27+
import org.springframework.boot.web.server.LocalServerPort
28+
import org.springframework.restdocs.restassured.RestAssuredRestDocumentation
29+
30+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
31+
@AutoConfigureRestDocs
32+
class MyUserDocumentationTests {
33+
34+
@Test
35+
fun listUsers(@Autowired documentationSpec: RequestSpecification?, @LocalServerPort port: Int) {
36+
RestAssured.given(documentationSpec)
37+
.filter(RestAssuredRestDocumentation.document("list-users"))
38+
.`when`()
39+
.port(port)["/"]
40+
.then().assertThat()
41+
.statusCode(Matchers.`is`(200))
42+
}
43+
44+
}

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
optional("org.springframework.restdocs:spring-restdocs-mockmvc") {
5050
exclude group: "javax.servlet", module: "javax.servlet-api"
5151
}
52+
optional("org.springframework.restdocs:spring-restdocs-restassured")
5253
optional("org.springframework.restdocs:spring-restdocs-webtestclient")
5354
optional("org.springframework.security:spring-security-config")
5455
optional("org.springframework.security:spring-security-test")

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/AutoConfigureRestDocs.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26+
import io.restassured.RestAssured;
27+
2628
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2729
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
2830
import org.springframework.context.annotation.Import;
@@ -33,19 +35,22 @@
3335
/**
3436
* Annotation that can be applied to a test class to enable and configure
3537
* auto-configuration of Spring REST Docs. The auto-configuration sets up
36-
* {@link MockMvc}-based testing of a servlet web application or
37-
* {@link WebTestClient}-based testing of a reactive web application.
38+
* {@link MockMvc}-based testing of a servlet web application, {@link WebTestClient}-based
39+
* testing of a reactive web application, or {@link RestAssured}-based testing of any web
40+
* application over HTTP.
3841
* <p>
3942
* Allows configuration of the output directory and the host, scheme, and port of
4043
* generated URIs. When further configuration is required a
41-
* {@link RestDocsMockMvcConfigurationCustomizer} or
42-
* {@link RestDocsWebTestClientConfigurationCustomizer} bean can be used.
44+
* {@link RestDocsMockMvcConfigurationCustomizer},
45+
* {@link RestDocsWebTestClientConfigurationCustomizer}, or
46+
* {@link RestDocsRestAssuredConfigurationCustomizer} bean can be used.
4347
*
4448
* @author Andy Wilkinson
4549
* @since 1.4.0
4650
* @see RestDocsAutoConfiguration
4751
* @see RestDocsMockMvcConfigurationCustomizer
4852
* @see RestDocsWebTestClientConfigurationCustomizer
53+
* @see RestDocsRestAssuredConfigurationCustomizer
4954
*/
5055
@Target(ElementType.TYPE)
5156
@Retention(RetentionPolicy.RUNTIME)

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.test.autoconfigure.restdocs;
1818

19+
import io.restassured.builder.RequestSpecBuilder;
20+
import io.restassured.specification.RequestSpecification;
21+
1922
import org.springframework.beans.factory.ObjectProvider;
2023
import org.springframework.boot.autoconfigure.AutoConfiguration;
2124
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -30,6 +33,8 @@
3033
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
3134
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
3235
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
36+
import org.springframework.restdocs.restassured.RestAssuredRestDocumentation;
37+
import org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer;
3338
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation;
3439
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer;
3540

@@ -72,6 +77,31 @@ RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(RestDocsProperties
7277

7378
}
7479

80+
@Configuration(proxyBeanMethods = false)
81+
@ConditionalOnClass({ RequestSpecification.class, RestAssuredRestDocumentation.class })
82+
@EnableConfigurationProperties(RestDocsProperties.class)
83+
static class RestDocsRestAssuredConfiguration {
84+
85+
@Bean
86+
@ConditionalOnMissingBean
87+
RequestSpecification restDocsRestAssuredConfigurer(
88+
ObjectProvider<RestDocsRestAssuredConfigurationCustomizer> configurationCustomizers,
89+
RestDocumentationContextProvider contextProvider) {
90+
RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation
91+
.documentationConfiguration(contextProvider);
92+
configurationCustomizers.orderedStream()
93+
.forEach((configurationCustomizer) -> configurationCustomizer.customize(configurer));
94+
return new RequestSpecBuilder().addFilter(configurer).build();
95+
}
96+
97+
@Bean
98+
RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(RestDocsProperties properties,
99+
RequestSpecification configurer) {
100+
return new RestDocsRestAssuredBuilderCustomizer(properties, configurer);
101+
}
102+
103+
}
104+
75105
@Configuration(proxyBeanMethods = false)
76106
@ConditionalOnClass(WebTestClientRestDocumentation.class)
77107
@ConditionalOnWebApplication(type = Type.REACTIVE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.test.autoconfigure.restdocs;
18+
19+
import io.restassured.specification.RequestSpecification;
20+
21+
import org.springframework.beans.factory.InitializingBean;
22+
import org.springframework.boot.context.properties.PropertyMapper;
23+
import org.springframework.util.StringUtils;
24+
25+
/**
26+
* A customizer that configures Spring REST Docs with REST Assured.
27+
*
28+
* @author Eddú Meléndez
29+
*/
30+
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
31+
32+
private final RestDocsProperties properties;
33+
34+
private final RequestSpecification delegate;
35+
36+
RestDocsRestAssuredBuilderCustomizer(RestDocsProperties properties, RequestSpecification delegate) {
37+
this.properties = properties;
38+
this.delegate = delegate;
39+
}
40+
41+
@Override
42+
public void afterPropertiesSet() throws Exception {
43+
PropertyMapper map = PropertyMapper.get();
44+
String host = this.properties.getUriHost();
45+
map.from(this.properties::getUriScheme)
46+
.when((scheme) -> StringUtils.hasText(scheme) && StringUtils.hasText(host))
47+
.to((scheme) -> this.delegate.baseUri(scheme + "://" + host));
48+
map.from(this.properties::getUriPort).whenNonNull().to(this.delegate::port);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)