Skip to content

Commit dd5435e

Browse files
committed
Add support to RestDocs to work with ResAssured
See gh-7228
1 parent f2fe2c4 commit dd5435e

File tree

8 files changed

+238
-3
lines changed

8 files changed

+238
-3
lines changed

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<quartz.version>2.3.0</quartz.version>
143143
<querydsl.version>4.1.4</querydsl.version>
144144
<reactor-bom.version>Bismuth-BUILD-SNAPSHOT</reactor-bom.version>
145+
<restassured.version>3.0.2</restassured.version>
145146
<rxjava.version>1.3.0</rxjava.version>
146147
<rxjava-adapter.version>1.2.1</rxjava-adapter.version>
147148
<rxjava2.version>2.1.0</rxjava2.version>
@@ -868,6 +869,11 @@
868869
<artifactId>rxjava</artifactId>
869870
<version>${rxjava2.version}</version>
870871
</dependency>
872+
<dependency>
873+
<groupId>io.rest-assured</groupId>
874+
<artifactId>rest-assured</artifactId>
875+
<version>${restassured.version}</version>
876+
</dependency>
871877
<dependency>
872878
<groupId>io.searchbox</groupId>
873879
<artifactId>jest</artifactId>

spring-boot-parent/src/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck" />
7171
<module name="com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck">
7272
<property name="excludes"
73-
value="org.assertj.core.api.Assertions.*, org.junit.Assert.*, org.junit.Assume.*, org.junit.internal.matchers.ThrowableMessageMatcher.*, org.hamcrest.CoreMatchers.*, org.hamcrest.Matchers.*, org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.*, org.springframework.boot.configurationprocessor.TestCompiler.*, org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.*, org.mockito.Mockito.*, org.mockito.BDDMockito.*, org.mockito.ArgumentMatchers.*, org.mockito.Matchers.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.hypermedia.HypermediaDocumentation.*, org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*, org.springframework.test.web.servlet.result.MockMvcResultMatchers.*, org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*, org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*, org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo, org.springframework.test.web.client.match.MockRestRequestMatchers.*, org.springframework.test.web.client.response.MockRestResponseCreators.*" />
73+
value="io.restassured.RestAssured.*, org.assertj.core.api.Assertions.*, org.junit.Assert.*, org.junit.Assume.*, org.junit.internal.matchers.ThrowableMessageMatcher.*, org.hamcrest.CoreMatchers.*, org.hamcrest.Matchers.*, org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.*, org.springframework.boot.configurationprocessor.TestCompiler.*, org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.*, org.mockito.Mockito.*, org.mockito.BDDMockito.*, org.mockito.ArgumentMatchers.*, org.mockito.Matchers.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.hypermedia.HypermediaDocumentation.*, org.springframework.restdocs.operation.preprocess.Preprocessors.*, org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.*, org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.*, org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*, org.springframework.test.web.servlet.result.MockMvcResultMatchers.*, org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*, org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*, org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo, org.springframework.test.web.client.match.MockRestRequestMatchers.*, org.springframework.test.web.client.response.MockRestResponseCreators.*" />
7474
</module>
7575
<module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck" />
7676
<module name="com.puppycrawl.tools.checkstyle.checks.imports.RedundantImportCheck" />

spring-boot-test-autoconfigure/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>json-path</artifactId>
5555
<optional>true</optional>
5656
</dependency>
57+
<dependency>
58+
<groupId>io.rest-assured</groupId>
59+
<artifactId>rest-assured</artifactId>
60+
<optional>true</optional>
61+
</dependency>
5762
<dependency>
5863
<groupId>net.sourceforge.htmlunit</groupId>
5964
<artifactId>htmlunit</artifactId>
@@ -141,6 +146,11 @@
141146
<artifactId>spring-restdocs-mockmvc</artifactId>
142147
<optional>true</optional>
143148
</dependency>
149+
<dependency>
150+
<groupId>org.springframework.restdocs</groupId>
151+
<artifactId>spring-restdocs-restassured</artifactId>
152+
<optional>true</optional>
153+
</dependency>
144154
<dependency>
145155
<groupId>org.springframework.security</groupId>
146156
<artifactId>spring-security-config</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2017 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.test.autoconfigure.restdocs;
18+
19+
import io.restassured.builder.RequestSpecBuilder;
20+
import io.restassured.specification.RequestSpecification;
21+
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
27+
import org.springframework.boot.context.properties.ConfigurationProperties;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.restdocs.RestDocumentationContextProvider;
32+
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation;
33+
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
34+
35+
/**
36+
* {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs with
37+
* RestAssured.
38+
*
39+
* @author Eddú Meléndez
40+
* @since 2.0.0
41+
*/
42+
@Configuration
43+
@ConditionalOnWebApplication(type = Type.SERVLET)
44+
@ConditionalOnClass({ RequestSpecification.class, RestAssuredRestDocumentation.class })
45+
@EnableConfigurationProperties
46+
public class RestAssuredAutoConfiguration {
47+
48+
@Bean
49+
@ConditionalOnMissingBean(RestAssuredRestDocumentationConfigurer.class)
50+
public RequestSpecification restDocsRestAssuredConfigurer(
51+
RestDocumentationContextProvider contextProvider) {
52+
RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation
53+
.documentationConfiguration(contextProvider);
54+
return new RequestSpecBuilder().addFilter(configurer).build();
55+
}
56+
57+
@Bean
58+
@ConfigurationProperties(prefix = "spring.test.restdocs")
59+
public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(
60+
RequestSpecification configurer) {
61+
return new RestDocsRestAssuredBuilderCustomizer(configurer);
62+
}
63+
64+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.beans.factory.ObjectProvider;
2020
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
@@ -34,10 +35,12 @@
3435
* {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs.
3536
*
3637
* @author Andy Wilkinson
38+
* @author Eddú Meléndez
3739
* @since 1.4.0
3840
*/
3941
@Configuration
4042
@ConditionalOnWebApplication(type = Type.SERVLET)
43+
@ConditionalOnClass(MockMvcRestDocumentation.class)
4144
@EnableConfigurationProperties
4245
public class RestDocsAutoConfiguration {
4346

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2017 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.test.autoconfigure.restdocs;
18+
19+
import io.restassured.specification.RequestSpecification;
20+
21+
import org.springframework.beans.factory.InitializingBean;
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* A customizer that configures Spring REST Docs with RestAssured.
26+
*
27+
* @author Eddú Meléndez
28+
*/
29+
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
30+
31+
private final RequestSpecification delegate;
32+
33+
private String uriScheme;
34+
35+
private String uriHost;
36+
37+
private Integer uriPort;
38+
39+
RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) {
40+
this.delegate = delegate;
41+
}
42+
43+
public String getUriScheme() {
44+
return this.uriScheme;
45+
}
46+
47+
public void setUriScheme(String uriScheme) {
48+
this.uriScheme = uriScheme;
49+
}
50+
51+
public String getUriHost() {
52+
return this.uriHost;
53+
}
54+
55+
public void setUriHost(String uriHost) {
56+
this.uriHost = uriHost;
57+
}
58+
59+
public Integer getUriPort() {
60+
return this.uriPort;
61+
}
62+
63+
public void setUriPort(Integer uriPort) {
64+
this.uriPort = uriPort;
65+
}
66+
67+
@Override
68+
public void afterPropertiesSet() throws Exception {
69+
if (StringUtils.hasText(this.uriScheme) && StringUtils.hasText(this.uriHost)) {
70+
this.delegate.baseUri(this.uriScheme + "://" + this.uriHost);
71+
}
72+
if (this.uriPort != null) {
73+
this.delegate.port(this.uriPort);
74+
}
75+
}
76+
77+
}

spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerAuto
9898

9999
# AutoConfigureRestDocs auto-configuration imports
100100
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=\
101-
org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration
101+
org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration,\
102+
org.springframework.boot.test.autoconfigure.restdocs.RestAssuredAutoConfiguration
102103

103104
# AutoConfigureTestEntityManager auto-configuration imports
104105
org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager=\
@@ -143,4 +144,4 @@ org.springframework.test.context.TestExecutionListener=\
143144
org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,\
144145
org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,\
145146
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,\
146-
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener
147+
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2017 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.test.autoconfigure.restdocs;
18+
19+
import java.io.File;
20+
21+
import io.restassured.specification.RequestSpecification;
22+
import org.assertj.core.api.Condition;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.test.context.junit4.SpringRunner;
29+
import org.springframework.util.FileSystemUtils;
30+
31+
import static io.restassured.RestAssured.given;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.hamcrest.CoreMatchers.is;
34+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
35+
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
36+
import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris;
37+
38+
/**
39+
* Tests for {@link RestAssuredAutoConfiguration}
40+
*
41+
* @author Eddú Meléndez
42+
*/
43+
@RunWith(SpringRunner.class)
44+
@AutoConfigureRestDocs
45+
public class RestAssuredAutoConfigurationIntegrationTests {
46+
47+
@Before
48+
public void deleteSnippets() {
49+
FileSystemUtils.deleteRecursively(new File("target/generated-snippets"));
50+
}
51+
52+
@Autowired
53+
private RequestSpecification documentationSpec;
54+
55+
@Test
56+
public void defaultSnippetsAreWritten() throws Exception {
57+
given(this.documentationSpec)
58+
.filter(document("default-snippets", preprocessRequest(modifyUris()
59+
.scheme("https").host("api.example.com").removePort()))).when()
60+
.get("/").then().assertThat().statusCode(is(200));
61+
File defaultSnippetsDir = new File("target/generated-snippets/default-snippets");
62+
assertThat(defaultSnippetsDir).exists();
63+
assertThat(new File(defaultSnippetsDir, "curl-request.adoc")).has(
64+
contentContaining("'https://api.example.com/'"));
65+
assertThat(new File(defaultSnippetsDir, "http-request.adoc")).has(
66+
contentContaining("api.example.com"));
67+
assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile();
68+
}
69+
70+
private Condition<File> contentContaining(String toContain) {
71+
return new ContentContainingCondition(toContain);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)