Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
98b2267a
Commit
98b2267a
authored
Jun 21, 2017
by
Eddú Meléndez
Committed by
Andy Wilkinson
Sep 25, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add auto-configuration for REST Docs with REST Assured
See gh-9643
parent
a1e26eae
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
379 additions
and
22 deletions
+379
-22
pom.xml
spring-boot-dependencies/pom.xml
+6
-0
checkstyle.xml
spring-boot-parent/src/checkstyle/checkstyle.xml
+1
-1
pom.xml
spring-boot-test-autoconfigure/pom.xml
+15
-0
RestDocsAutoConfiguration.java
...est/autoconfigure/restdocs/RestDocsAutoConfiguration.java
+61
-20
RestDocsRestAssuredBuilderCustomizer.java
...figure/restdocs/RestDocsRestAssuredBuilderCustomizer.java
+77
-0
RestDocsRestAssuredConfigurationCustomizer.java
.../restdocs/RestDocsRestAssuredConfigurationCustomizer.java
+41
-0
RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests.java
...toConfigurationAdvancedConfigurationIntegrationTests.java
+95
-0
RestAssuredAutoConfigurationIntegrationTests.java
...estdocs/RestAssuredAutoConfigurationIntegrationTests.java
+81
-0
RestDocsTestApplication.java
.../test/autoconfigure/restdocs/RestDocsTestApplication.java
+2
-1
No files found.
spring-boot-dependencies/pom.xml
View file @
98b2267a
...
@@ -150,6 +150,7 @@
...
@@ -150,6 +150,7 @@
<quartz.version>
2.3.0
</quartz.version>
<quartz.version>
2.3.0
</quartz.version>
<querydsl.version>
4.1.4
</querydsl.version>
<querydsl.version>
4.1.4
</querydsl.version>
<reactor-bom.version>
Bismuth-RC1
</reactor-bom.version>
<reactor-bom.version>
Bismuth-RC1
</reactor-bom.version>
<rest-assured.version>
3.0.2
</rest-assured.version>
<reactive-streams.version>
1.0.1
</reactive-streams.version>
<reactive-streams.version>
1.0.1
</reactive-streams.version>
<rxjava.version>
1.3.2
</rxjava.version>
<rxjava.version>
1.3.2
</rxjava.version>
<rxjava-adapter.version>
1.2.1
</rxjava-adapter.version>
<rxjava-adapter.version>
1.2.1
</rxjava-adapter.version>
...
@@ -906,6 +907,11 @@
...
@@ -906,6 +907,11 @@
<artifactId>
rxjava
</artifactId>
<artifactId>
rxjava
</artifactId>
<version>
${rxjava2.version}
</version>
<version>
${rxjava2.version}
</version>
</dependency>
</dependency>
<dependency>
<groupId>
io.rest-assured
</groupId>
<artifactId>
rest-assured
</artifactId>
<version>
${rest-assured.version}
</version>
</dependency>
<dependency>
<dependency>
<groupId>
io.searchbox
</groupId>
<groupId>
io.searchbox
</groupId>
<artifactId>
jest
</artifactId>
<artifactId>
jest
</artifactId>
...
...
spring-boot-parent/src/checkstyle/checkstyle.xml
View file @
98b2267a
...
@@ -70,7 +70,7 @@
...
@@ -70,7 +70,7 @@
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck"
/>
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck"
/>
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck"
>
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.AvoidStaticImportCheck"
>
<property
name=
"excludes"
<property
name=
"excludes"
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.Hypermedia
Documentation.*, 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.ExpectedCount.*, org.springframework.test.web.client.match.MockRestRequestMatchers.*, org.springframework.test.web.client.response.MockRestResponseCreators.*"
/>
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.hypermedia.HypermediaDocumentation.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.operation.preprocess.Preprocessors.*, org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.*, org.springframework.restdocs.restassured3.RestAssuredRest
Documentation.*, 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.ExpectedCount.*, org.springframework.test.web.client.match.MockRestRequestMatchers.*, org.springframework.test.web.client.response.MockRestResponseCreators.*"
/>
</module>
</module>
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck"
>
<module
name=
"com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck"
>
<property
name=
"illegalPkgs"
value=
"com.google.common"
/>
<property
name=
"illegalPkgs"
value=
"com.google.common"
/>
...
...
spring-boot-test-autoconfigure/pom.xml
View file @
98b2267a
...
@@ -59,6 +59,11 @@
...
@@ -59,6 +59,11 @@
<artifactId>
json-path
</artifactId>
<artifactId>
json-path
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
io.rest-assured
</groupId>
<artifactId>
rest-assured
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
net.sourceforge.htmlunit
</groupId>
<groupId>
net.sourceforge.htmlunit
</groupId>
<artifactId>
htmlunit
</artifactId>
<artifactId>
htmlunit
</artifactId>
...
@@ -146,6 +151,11 @@
...
@@ -146,6 +151,11 @@
<artifactId>
spring-restdocs-mockmvc
</artifactId>
<artifactId>
spring-restdocs-mockmvc
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.restdocs
</groupId>
<artifactId>
spring-restdocs-restassured
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.security
</groupId>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-config
</artifactId>
<artifactId>
spring-security-config
</artifactId>
...
@@ -198,6 +208,11 @@
...
@@ -198,6 +208,11 @@
<artifactId>
commons-pool2
</artifactId>
<artifactId>
commons-pool2
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.tomcat.embed
</groupId>
<artifactId>
tomcat-embed-core
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.aspectj
</groupId>
<groupId>
org.aspectj
</groupId>
<artifactId>
aspectjrt
</artifactId>
<artifactId>
aspectjrt
</artifactId>
...
...
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java
View file @
98b2267a
...
@@ -16,8 +16,12 @@
...
@@ -16,8 +16,12 @@
package
org
.
springframework
.
boot
.
test
.
autoconfigure
.
restdocs
;
package
org
.
springframework
.
boot
.
test
.
autoconfigure
.
restdocs
;
import
io.restassured.builder.RequestSpecBuilder
;
import
io.restassured.specification.RequestSpecification
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type
;
...
@@ -29,40 +33,77 @@ import org.springframework.restdocs.RestDocumentationContextProvider;
...
@@ -29,40 +33,77 @@ import org.springframework.restdocs.RestDocumentationContextProvider;
import
org.springframework.restdocs.mockmvc.MockMvcRestDocumentation
;
import
org.springframework.restdocs.mockmvc.MockMvcRestDocumentation
;
import
org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer
;
import
org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer
;
import
org.springframework.restdocs.mockmvc.RestDocumentationResultHandler
;
import
org.springframework.restdocs.mockmvc.RestDocumentationResultHandler
;
import
org.springframework.restdocs.restassured3.RestAssuredRestDocumentation
;
import
org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer
;
/**
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs.
* {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs.
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Eddú Meléndez
* @since 1.4.0
* @since 1.4.0
*/
*/
@Configuration
@Configuration
@ConditionalOnWebApplication
(
type
=
Type
.
SERVLET
)
@EnableConfigurationProperties
@EnableConfigurationProperties
public
class
RestDocsAutoConfiguration
{
public
class
RestDocsAutoConfiguration
{
@Bean
@Configuration
@ConditionalOnMissingBean
(
MockMvcRestDocumentationConfigurer
.
class
)
@ConditionalOnWebApplication
(
type
=
Type
.
SERVLET
)
public
MockMvcRestDocumentationConfigurer
restDocsMockMvcConfigurer
(
@ConditionalOnClass
(
MockMvcRestDocumentation
.
class
)
ObjectProvider
<
RestDocsMockMvcConfigurationCustomizer
>
configurationCustomizerProvider
,
static
class
RestDocsMockMvcAutoConfiguration
{
RestDocumentationContextProvider
contextProvider
)
{
MockMvcRestDocumentationConfigurer
configurer
=
MockMvcRestDocumentation
@Bean
.
documentationConfiguration
(
contextProvider
);
@ConditionalOnMissingBean
(
MockMvcRestDocumentationConfigurer
.
class
)
RestDocsMockMvcConfigurationCustomizer
configurationCustomizer
=
configurationCustomizerProvider
public
MockMvcRestDocumentationConfigurer
restDocsMockMvcConfigurer
(
.
getIfAvailable
();
ObjectProvider
<
RestDocsMockMvcConfigurationCustomizer
>
configurationCustomizerProvider
,
if
(
configurationCustomizer
!=
null
)
{
RestDocumentationContextProvider
contextProvider
)
{
configurationCustomizer
.
customize
(
configurer
);
MockMvcRestDocumentationConfigurer
configurer
=
MockMvcRestDocumentation
.
documentationConfiguration
(
contextProvider
);
RestDocsMockMvcConfigurationCustomizer
configurationCustomizer
=
configurationCustomizerProvider
.
getIfAvailable
();
if
(
configurationCustomizer
!=
null
)
{
configurationCustomizer
.
customize
(
configurer
);
}
return
configurer
;
}
@Bean
@ConfigurationProperties
(
prefix
=
"spring.test.restdocs"
)
public
RestDocsMockMvcBuilderCustomizer
restDocumentationConfigurer
(
MockMvcRestDocumentationConfigurer
configurer
,
ObjectProvider
<
RestDocumentationResultHandler
>
resultHandler
)
{
return
new
RestDocsMockMvcBuilderCustomizer
(
configurer
,
resultHandler
.
getIfAvailable
());
}
}
return
configurer
;
}
}
@Bean
@Configuration
@ConfigurationProperties
(
prefix
=
"spring.test.restdocs"
)
@ConditionalOnClass
({
RequestSpecification
.
class
,
RestAssuredRestDocumentation
.
class
})
public
RestDocsMockMvcBuilderCustomizer
restDocumentationConfigurer
(
static
class
RestDocsRestAssuredAutoConfiguration
{
MockMvcRestDocumentationConfigurer
configurer
,
ObjectProvider
<
RestDocumentationResultHandler
>
resultHandler
)
{
@Bean
return
new
RestDocsMockMvcBuilderCustomizer
(
configurer
,
@ConditionalOnMissingBean
(
RequestSpecification
.
class
)
resultHandler
.
getIfAvailable
());
public
RequestSpecification
restDocsRestAssuredConfigurer
(
ObjectProvider
<
RestDocsRestAssuredConfigurationCustomizer
>
configurationCustomizerProvider
,
RestDocumentationContextProvider
contextProvider
)
{
RestAssuredRestDocumentationConfigurer
configurer
=
RestAssuredRestDocumentation
.
documentationConfiguration
(
contextProvider
);
RestDocsRestAssuredConfigurationCustomizer
configurationCustomizer
=
configurationCustomizerProvider
.
getIfAvailable
();
if
(
configurationCustomizer
!=
null
)
{
configurationCustomizer
.
customize
(
configurer
);
}
return
new
RequestSpecBuilder
().
addFilter
(
configurer
).
build
();
}
@Bean
@ConfigurationProperties
(
prefix
=
"spring.test.restdocs"
)
public
RestDocsRestAssuredBuilderCustomizer
restAssuredBuilderCustomizer
(
RequestSpecification
configurer
)
{
return
new
RestDocsRestAssuredBuilderCustomizer
(
configurer
);
}
}
}
}
}
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java
0 → 100644
View file @
98b2267a
/*
* Copyright 2012-2017 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
.
autoconfigure
.
restdocs
;
import
io.restassured.specification.RequestSpecification
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.util.StringUtils
;
/**
* A customizer that configures Spring REST Docs with RestAssured.
*
* @author Eddú Meléndez
*/
class
RestDocsRestAssuredBuilderCustomizer
implements
InitializingBean
{
private
final
RequestSpecification
delegate
;
private
String
uriScheme
;
private
String
uriHost
;
private
Integer
uriPort
;
RestDocsRestAssuredBuilderCustomizer
(
RequestSpecification
delegate
)
{
this
.
delegate
=
delegate
;
}
public
String
getUriScheme
()
{
return
this
.
uriScheme
;
}
public
void
setUriScheme
(
String
uriScheme
)
{
this
.
uriScheme
=
uriScheme
;
}
public
String
getUriHost
()
{
return
this
.
uriHost
;
}
public
void
setUriHost
(
String
uriHost
)
{
this
.
uriHost
=
uriHost
;
}
public
Integer
getUriPort
()
{
return
this
.
uriPort
;
}
public
void
setUriPort
(
Integer
uriPort
)
{
this
.
uriPort
=
uriPort
;
}
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
if
(
StringUtils
.
hasText
(
this
.
uriScheme
)
&&
StringUtils
.
hasText
(
this
.
uriHost
))
{
this
.
delegate
.
baseUri
(
this
.
uriScheme
+
"://"
+
this
.
uriHost
);
}
if
(
this
.
uriPort
!=
null
)
{
this
.
delegate
.
port
(
this
.
uriPort
);
}
}
}
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredConfigurationCustomizer.java
0 → 100644
View file @
98b2267a
/*
* Copyright 2012-2017 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
.
autoconfigure
.
restdocs
;
import
org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer
;
/**
* A customizer for {@link RestAssuredRestDocumentationConfigurer}. If a
* {@code RestDocsRestAssuredConfigurationCustomizer} bean is found in the application
* context it will be {@link #customize called} to customize the
* {@code RestAssuredRestDocumentationConfigurer} before it is applied. Intended for use
* only when the attributes on {@link AutoConfigureRestDocs} do not provide sufficient
* customization.
*
* @author Eddú Meléndez
* @since 2.0.0
*/
@FunctionalInterface
public
interface
RestDocsRestAssuredConfigurationCustomizer
{
/**
* Customize the given {@code configurer}.
* @param configurer the configurer
*/
void
customize
(
RestAssuredRestDocumentationConfigurer
configurer
);
}
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests.java
0 → 100644
View file @
98b2267a
/*
* Copyright 2012-2017 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
.
autoconfigure
.
restdocs
;
import
java.io.File
;
import
io.restassured.specification.RequestSpecification
;
import
org.assertj.core.api.Condition
;
import
org.junit.Before
;
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.boot.test.context.TestConfiguration
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer
;
import
org.springframework.restdocs.templates.TemplateFormats
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.util.FileSystemUtils
;
import
static
io
.
restassured
.
RestAssured
.
given
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
springframework
.
restdocs
.
operation
.
preprocess
.
Preprocessors
.
preprocessRequest
;
import
static
org
.
springframework
.
restdocs
.
restassured3
.
RestAssuredRestDocumentation
.
document
;
import
static
org
.
springframework
.
restdocs
.
restassured3
.
operation
.
preprocess
.
RestAssuredPreprocessors
.
modifyUris
;
/**
* Tests for {@link AutoConfigureRestDocs}.
*
* @author Eddú Meléndez
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@AutoConfigureRestDocs
public
class
RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests
{
@LocalServerPort
private
int
port
;
@Before
public
void
deleteSnippets
()
{
FileSystemUtils
.
deleteRecursively
(
new
File
(
"target/generated-snippets"
));
}
@Autowired
private
RequestSpecification
documentationSpec
;
@Test
public
void
snippetGeneration
()
throws
Exception
{
given
(
this
.
documentationSpec
)
.
filter
(
document
(
"default-snippets"
,
preprocessRequest
(
modifyUris
()
.
scheme
(
"https"
).
host
(
"api.example.com"
).
removePort
()))).
when
()
.
port
(
this
.
port
).
get
(
"/"
).
then
().
assertThat
().
statusCode
(
is
(
200
));
File
defaultSnippetsDir
=
new
File
(
"target/generated-snippets/default-snippets"
);
assertThat
(
defaultSnippetsDir
).
exists
();
assertThat
(
new
File
(
defaultSnippetsDir
,
"curl-request.md"
)).
has
(
contentContaining
(
"'https://api.example.com/'"
));
assertThat
(
new
File
(
defaultSnippetsDir
,
"http-request.md"
)).
has
(
contentContaining
(
"api.example.com"
));
assertThat
(
new
File
(
defaultSnippetsDir
,
"http-response.md"
)).
isFile
();
}
private
Condition
<
File
>
contentContaining
(
String
toContain
)
{
return
new
ContentContainingCondition
(
toContain
);
}
@TestConfiguration
public
static
class
CustomizationConfiguration
implements
RestDocsRestAssuredConfigurationCustomizer
{
@Override
public
void
customize
(
RestAssuredRestDocumentationConfigurer
configurer
)
{
configurer
.
snippets
().
withTemplateFormat
(
TemplateFormats
.
markdown
());
}
}
}
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredAutoConfigurationIntegrationTests.java
0 → 100644
View file @
98b2267a
/*
* Copyright 2012-2017 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
.
autoconfigure
.
restdocs
;
import
java.io.File
;
import
io.restassured.specification.RequestSpecification
;
import
org.assertj.core.api.Condition
;
import
org.junit.Before
;
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.boot.web.server.LocalServerPort
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.util.FileSystemUtils
;
import
static
io
.
restassured
.
RestAssured
.
given
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
springframework
.
restdocs
.
operation
.
preprocess
.
Preprocessors
.
preprocessRequest
;
import
static
org
.
springframework
.
restdocs
.
restassured3
.
RestAssuredRestDocumentation
.
document
;
import
static
org
.
springframework
.
restdocs
.
restassured3
.
operation
.
preprocess
.
RestAssuredPreprocessors
.
modifyUris
;
/**
* Tests for {@link RestDocsAutoConfiguration}
*
* @author Eddú Meléndez
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@AutoConfigureRestDocs
public
class
RestAssuredAutoConfigurationIntegrationTests
{
@LocalServerPort
private
int
port
;
@Before
public
void
deleteSnippets
()
{
FileSystemUtils
.
deleteRecursively
(
new
File
(
"target/generated-snippets"
));
}
@Autowired
private
RequestSpecification
documentationSpec
;
@Test
public
void
defaultSnippetsAreWritten
()
throws
Exception
{
given
(
this
.
documentationSpec
)
.
filter
(
document
(
"default-snippets"
,
preprocessRequest
(
modifyUris
()
.
scheme
(
"https"
).
host
(
"api.example.com"
).
removePort
()))).
when
()
.
port
(
this
.
port
).
get
(
"/"
).
then
().
assertThat
().
statusCode
(
is
(
200
));
File
defaultSnippetsDir
=
new
File
(
"target/generated-snippets/default-snippets"
);
assertThat
(
defaultSnippetsDir
).
exists
();
assertThat
(
new
File
(
defaultSnippetsDir
,
"curl-request.adoc"
)).
has
(
contentContaining
(
"'https://api.example.com/'"
));
assertThat
(
new
File
(
defaultSnippetsDir
,
"http-request.adoc"
)).
has
(
contentContaining
(
"api.example.com"
));
assertThat
(
new
File
(
defaultSnippetsDir
,
"http-response.adoc"
)).
isFile
();
}
private
Condition
<
File
>
contentContaining
(
String
toContain
)
{
return
new
ContentContainingCondition
(
toContain
);
}
}
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java
View file @
98b2267a
...
@@ -17,13 +17,14 @@
...
@@ -17,13 +17,14 @@
package
org
.
springframework
.
boot
.
test
.
autoconfigure
.
restdocs
;
package
org
.
springframework
.
boot
.
test
.
autoconfigure
.
restdocs
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
;
/**
/**
* Test application used with {@link AutoConfigureRestDocs} tests.
* Test application used with {@link AutoConfigureRestDocs} tests.
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
*/
*/
@SpringBootApplication
@SpringBootApplication
(
exclude
=
SecurityAutoConfiguration
.
class
)
public
class
RestDocsTestApplication
{
public
class
RestDocsTestApplication
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment