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
352e3ca3
Commit
352e3ca3
authored
Sep 25, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Add auto-configuration for REST Docs with REST Assured"
Closes gh-9643
parent
98b2267a
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
175 additions
and
36 deletions
+175
-36
pom.xml
spring-boot-docs/pom.xml
+8
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+40
-7
AdvancedConfigurationExample.java
...re/restdocs/restassured/AdvancedConfigurationExample.java
+39
-0
UserDocumentationTests.java
...onfigure/restdocs/restassured/UserDocumentationTests.java
+53
-0
RestDocsAutoConfiguration.java
...est/autoconfigure/restdocs/RestDocsAutoConfiguration.java
+6
-4
RestDocsRestAssuredBuilderCustomizer.java
...figure/restdocs/RestDocsRestAssuredBuilderCustomizer.java
+1
-1
MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
...toConfigurationAdvancedConfigurationIntegrationTests.java
+3
-2
MockMvcRestDocsAutoConfigurationIntegrationTests.java
...ocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java
+2
-2
RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
...toConfigurationAdvancedConfigurationIntegrationTests.java
+13
-11
RestAssuredRestDocsAutoConfigurationIntegrationTests.java
...RestAssuredRestDocsAutoConfigurationIntegrationTests.java
+10
-9
No files found.
spring-boot-docs/pom.xml
View file @
352e3ca3
...
...
@@ -55,6 +55,14 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-test-autoconfigure
</artifactId>
</dependency>
<dependency>
<groupId>
io.rest-assured
</groupId>
<artifactId>
rest-assured
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.restdocs
</groupId>
<artifactId>
spring-restdocs-restassured
</artifactId>
</dependency>
<!-- Optional deps required when generating Javadoc with Java 8 -->
<dependency>
<groupId>
ch.qos.logback
</groupId>
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
352e3ca3
...
...
@@ -6279,8 +6279,20 @@ A list of the auto-configuration that is enabled by `@RestClientTest` can be
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]]
==== Auto-configured Spring REST Docs tests
The `@AutoConfigureRestDocs` annotation can be used if you want to use Spring REST Docs
in your tests. It will automatically configure `MockMvc` to use Spring REST Docs and
remove the need for Spring REST Docs' JUnit rule.
in your tests with Mock MVC or REST Assured. It removes the need for Spring REST Docs'
JUnit rule.
`@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). It can also be used to configure the host, scheme, and port that will
appear in any documented URIs.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-mock-mvc]]
===== Auto-configured Spring REST Docs tests with Mock MVC
`@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs, Inject it
using `@Autowired` and use it in your tests as you normally would when using Mock MVC and
Spring REST Docs:
[source,java,indent=0]
----
...
...
@@ -6315,11 +6327,9 @@ remove the need for Spring REST Docs' JUnit rule.
}
----
`@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). It can also be used to configure the host, scheme, and port that will
appear in any documented URIs. If you require more control over Spring REST Docs'
configuration a `RestDocsMockMvcConfigurationCustomizer` bean can be used:
If you require more control over Spring REST Docs' configuration than offered by the
attributes of `@AutoConfigureRestDocs`, a `RestDocsMockMvcConfigurationCustomizer` bean
can be used:
[source,java,indent=0]
----
...
...
@@ -6355,6 +6365,29 @@ automatically generate the default snippets:
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured]]
===== Auto-configured Spring REST Docs tests with REST Assured
`@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring REST
Docs, available to your tests. Inject it using `@Autowired` and use it in your tests as you
normally would when using REST Assured and Spring REST Docs:
[source,java,indent=0]
----
include::{code-examples}/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java[tag=source]
----
If you require more control over Spring REST Docs' configuration than offered by the
attributes of `@AutoConfigureRestDocs`, a `RestDocsRestAssuredConfigurationCustomizer`
bean can be used:
[source,java,indent=0]
----
include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java[tag=configuration]
----
[[boot-features-testing-spring-boot-applications-with-spock]]
==== Using Spock to test Spring Boot applications
If you wish to use Spock to test a Spring Boot application you should add a dependency
...
...
spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java
0 → 100644
View file @
352e3ca3
/*
* 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
.
restassured
;
import
org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer
;
import
org.springframework.boot.test.context.TestConfiguration
;
import
org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer
;
import
org.springframework.restdocs.templates.TemplateFormats
;
public
class
AdvancedConfigurationExample
{
// tag::configuration[]
@TestConfiguration
public
static
class
CustomizationConfiguration
implements
RestDocsRestAssuredConfigurationCustomizer
{
@Override
public
void
customize
(
RestAssuredRestDocumentationConfigurer
configurer
)
{
configurer
.
snippets
().
withTemplateFormat
(
TemplateFormats
.
markdown
());
}
}
// end::configuration[]
}
spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java
0 → 100644
View file @
352e3ca3
/*
* 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
.
restassured
;
// tag::source[]
import
io.restassured.specification.RequestSpecification
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
;
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
static
io
.
restassured
.
RestAssured
.
given
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
springframework
.
restdocs
.
restassured3
.
RestAssuredRestDocumentation
.
document
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@AutoConfigureRestDocs
public
class
UserDocumentationTests
{
@LocalServerPort
private
int
port
;
@Autowired
private
RequestSpecification
documentationSpec
;
@Test
public
void
listUsers
()
throws
Exception
{
given
(
this
.
documentationSpec
).
filter
(
document
(
"list-users"
)).
when
()
.
port
(
this
.
port
).
get
(
"/"
).
then
().
assertThat
().
statusCode
(
is
(
200
));
}
}
// end::source[]
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java
View file @
352e3ca3
...
...
@@ -45,11 +45,12 @@ import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationCon
*/
@Configuration
@EnableConfigurationProperties
@ConditionalOnWebApplication
public
class
RestDocsAutoConfiguration
{
@Configuration
@ConditionalOnWebApplication
(
type
=
Type
.
SERVLET
)
@ConditionalOnClass
(
MockMvcRestDocumentation
.
class
)
@ConditionalOnWebApplication
(
type
=
Type
.
SERVLET
)
static
class
RestDocsMockMvcAutoConfiguration
{
@Bean
...
...
@@ -79,7 +80,8 @@ public class RestDocsAutoConfiguration {
}
@Configuration
@ConditionalOnClass
({
RequestSpecification
.
class
,
RestAssuredRestDocumentation
.
class
})
@ConditionalOnClass
({
RequestSpecification
.
class
,
RestAssuredRestDocumentation
.
class
})
static
class
RestDocsRestAssuredAutoConfiguration
{
@Bean
...
...
@@ -89,7 +91,8 @@ public class RestDocsAutoConfiguration {
RestDocumentationContextProvider
contextProvider
)
{
RestAssuredRestDocumentationConfigurer
configurer
=
RestAssuredRestDocumentation
.
documentationConfiguration
(
contextProvider
);
RestDocsRestAssuredConfigurationCustomizer
configurationCustomizer
=
configurationCustomizerProvider
.
getIfAvailable
();
RestDocsRestAssuredConfigurationCustomizer
configurationCustomizer
=
configurationCustomizerProvider
.
getIfAvailable
();
if
(
configurationCustomizer
!=
null
)
{
configurationCustomizer
.
customize
(
configurer
);
}
...
...
@@ -105,5 +108,4 @@ public class RestDocsAutoConfiguration {
}
}
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java
View file @
352e3ca3
...
...
@@ -22,7 +22,7 @@ import org.springframework.beans.factory.InitializingBean;
import
org.springframework.util.StringUtils
;
/**
* A customizer that configures Spring REST Docs with R
est
Assured.
* A customizer that configures Spring REST Docs with R
EST
Assured.
*
* @author Eddú Meléndez
*/
...
...
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
→
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/
MockMvc
RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
View file @
352e3ca3
...
...
@@ -41,14 +41,15 @@ import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.li
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
/**
* Tests for {@link AutoConfigureRestDocs}.
* Integration tests for advanced configuration of {@link AutoConfigureRestDocs} with Mock
* MVC.
*
* @author Andy Wilkinson
*/
@RunWith
(
SpringRunner
.
class
)
@WebMvcTest
(
controllers
=
RestDocsTestController
.
class
,
secure
=
false
)
@AutoConfigureRestDocs
public
class
RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests
{
public
class
MockMvc
RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests
{
@Before
public
void
deleteSnippets
()
{
...
...
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java
→
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/
MockMvc
RestDocsAutoConfigurationIntegrationTests.java
View file @
352e3ca3
...
...
@@ -34,14 +34,14 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.docu
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
/**
*
Tests for {@link RestDocsAutoConfiguration}
.
*
Integration tests for {@link RestDocsAutoConfiguration} with Mock MVC
.
*
* @author Andy Wilkinson
*/
@RunWith
(
SpringRunner
.
class
)
@WebMvcTest
@AutoConfigureRestDocs
(
uriScheme
=
"https"
,
uriHost
=
"api.example.com"
,
uriPort
=
443
)
public
class
RestDocsAutoConfigurationIntegrationTests
{
public
class
MockMvc
RestDocsAutoConfigurationIntegrationTests
{
@Before
public
void
deleteSnippets
()
{
...
...
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests.java
→
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssured
RestDocs
AutoConfigurationAdvancedConfigurationIntegrationTests.java
View file @
352e3ca3
...
...
@@ -42,14 +42,15 @@ import static org.springframework.restdocs.restassured3.RestAssuredRestDocumenta
import
static
org
.
springframework
.
restdocs
.
restassured3
.
operation
.
preprocess
.
RestAssuredPreprocessors
.
modifyUris
;
/**
* Tests for {@link AutoConfigureRestDocs}.
* Integration tests for advanced configuration of {@link AutoConfigureRestDocs} with REST
* Assured.
*
* @author Eddú Meléndez
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@AutoConfigureRestDocs
public
class
RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests
{
public
class
RestAssured
RestDocs
AutoConfigurationAdvancedConfigurationIntegrationTests
{
@LocalServerPort
private
int
port
;
...
...
@@ -65,15 +66,16 @@ public class RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests {
@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
));
.
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
,
"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
();
}
...
...
@@ -82,8 +84,8 @@ public class RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests {
}
@TestConfiguration
public
static
class
CustomizationConfiguration
implements
RestDocsRestAssuredConfigurationCustomizer
{
public
static
class
CustomizationConfiguration
implements
RestDocsRestAssuredConfigurationCustomizer
{
@Override
public
void
customize
(
RestAssuredRestDocumentationConfigurer
configurer
)
{
...
...
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredAutoConfigurationIntegrationTests.java
→
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssured
RestDocs
AutoConfigurationIntegrationTests.java
View file @
352e3ca3
...
...
@@ -39,14 +39,14 @@ import static org.springframework.restdocs.restassured3.RestAssuredRestDocumenta
import
static
org
.
springframework
.
restdocs
.
restassured3
.
operation
.
preprocess
.
RestAssuredPreprocessors
.
modifyUris
;
/**
*
Tests for {@link RestDocsAutoConfiguration}
*
Integration tests for {@link RestDocsAutoConfiguration} with REST Assured.
*
* @author Eddú Meléndez
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
)
@AutoConfigureRestDocs
public
class
RestAssuredAutoConfigurationIntegrationTests
{
public
class
RestAssured
RestDocs
AutoConfigurationIntegrationTests
{
@LocalServerPort
private
int
port
;
...
...
@@ -62,15 +62,16 @@ public class RestAssuredAutoConfigurationIntegrationTests {
@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
));
.
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
,
"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
();
}
...
...
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