Document auto-configuration classes imported by each @…Test annotation

Closes gh-6044
pull/6323/merge
Andy Wilkinson 8 years ago
parent 5bfc6a50fd
commit 5c44c77287

@ -886,6 +886,25 @@
</artifactItems>
</configuration>
</execution>
<execution>
<id>unpack-test-slices</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>${project.version}</version>
<outputDirectory>
${project.build.directory}/test-auto-config
</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
@ -932,6 +951,7 @@
<scripts>
<script>file:///${project.basedir}/src/main/groovy/generateAutoConfigurationClassTables.groovy</script>
<script>file:///${project.basedir}/src/main/groovy/generateStarterTables.groovy</script>
<script>file:///${project.basedir}/src/main/groovy/generateTestSlicesTable.groovy</script>
</scripts>
</configuration>
<dependencies>
@ -940,6 +960,11 @@
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>

@ -0,0 +1,7 @@
[appendix]
[[test-auto-configuration]]
== Test auto-configuration annotations
Here is a table of the various `@…Test` annotations that can be used to test
slices of your application and the auto-configuration that they import by default:
include::../../../target/generated-resources/test-slice-auto-configuration.adoc[]

@ -4,5 +4,6 @@
include::appendix-application-properties.adoc[]
include::appendix-configuration-metadata.adoc[]
include::appendix-auto-configuration-classes.adoc[]
include::appendix-test-auto-configuration.adoc[]
include::appendix-executable-jar-format.adoc[]
include::appendix-dependency-versions.adoc[]

@ -4879,6 +4879,9 @@ NOTE: JSON helper classes can also be used directly in standard unit tests. Simp
call the `initFields` method of the helper in your `@Before` method if you aren't using
`@JsonTest`.
A list of the auto-configuration that is enabled by `@JsonTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests]]
@ -4971,6 +4974,9 @@ and/or a `WebDriver` bean. Here is an example that uses HtmlUnit:
}
----
A list of the auto-configuration that is enabled by `@WebMvcTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
@ -5053,6 +5059,8 @@ database you can use the `@AutoConfigureTestDatabase` annotation:
}
----
A list of the auto-configuration that is enabled by `@DataJpaTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
@ -5088,6 +5096,9 @@ be specified using `value` or `components` attribute of `@RestClientTest`:
}
----
A list of the auto-configuration that is enabled by `@RestClientTest` can be
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]]

@ -0,0 +1,109 @@
import groovy.io.FileType
import java.util.Properties
import org.springframework.core.io.InputStreamResource
import org.springframework.core.io.Resource
import org.springframework.core.type.AnnotationMetadata
import org.springframework.core.type.ClassMetadata
import org.springframework.core.type.classreading.MetadataReader
import org.springframework.core.type.classreading.MetadataReaderFactory
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory
import org.springframework.util.ClassUtils
import org.springframework.util.StringUtils
class Project {
final List<File> classFiles
final Properties springFactories
Project(File rootDirectory) {
this.springFactories = loadSpringFactories(rootDirectory)
this.classFiles = []
rootDirectory.eachFileRecurse (FileType.FILES) { file ->
if (file.name.endsWith('.class')) {
classFiles << file
}
}
}
private static Properties loadSpringFactories(File rootDirectory) {
Properties springFactories = new Properties()
new File(rootDirectory, 'META-INF/spring.factories').withInputStream { inputStream ->
springFactories.load(inputStream)
}
return springFactories
}
}
class TestSlice {
final String name
final SortedSet<String> importedAutoConfiguration
TestSlice(String annotationName, Collection<String> importedAutoConfiguration) {
this.name = ClassUtils.getShortName(annotationName)
this.importedAutoConfiguration = new TreeSet<String>(importedAutoConfiguration)
}
}
List<TestSlice> createTestSlices(Project project) {
MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory()
project.classFiles
.findAll { classFile ->
classFile.name.endsWith('Test.class')
}.collect { classFile ->
createMetadataReader(metadataReaderFactory, classFile)
}.findAll { metadataReader ->
metadataReader.classMetadata.annotation
}.collect { metadataReader ->
createTestSlice(project.springFactories, metadataReader.classMetadata, metadataReader.annotationMetadata)
}.sort {
a, b -> a.name.compareTo b.name
}
}
MetadataReader createMetadataReader(MetadataReaderFactory factory, File classFile) {
classFile.withInputStream { inputStream ->
factory.getMetadataReader(new InputStreamResource(inputStream))
}
}
TestSlice createTestSlice(Properties springFactories, ClassMetadata classMetadata, AnnotationMetadata annotationMetadata) {
new TestSlice(classMetadata.className, getImportedAutoConfiguration(springFactories, classMetadata, annotationMetadata))
}
Set<String> getImportedAutoConfiguration(Properties springFactories, ClassMetadata classMetadata, AnnotationMetadata annotationMetadata) {
annotationMetadata.annotationTypes
.findAll { annotationType ->
isAutoConfigurationImporter(annotationType, annotationMetadata)
}.collect { autoConfigurationImporter ->
StringUtils.commaDelimitedListToSet(springFactories.get(autoConfigurationImporter))
}.flatten()
}
boolean isAutoConfigurationImporter(String annotationType, AnnotationMetadata metadata) {
metadata.getMetaAnnotationTypes(annotationType).contains('org.springframework.boot.autoconfigure.ImportAutoConfiguration')
}
void writeTestSlicesTable(List<TestSlice> testSlices) {
new File(project.build.directory, "generated-resources/test-slice-auto-configuration.adoc").withPrintWriter { writer ->
writer.println '[cols="d,a"]'
writer.println '|==='
writer.println '| Test slice | Imported auto-configuration'
testSlices.each { testSlice ->
writer.println ''
writer.println "| `@${testSlice.name}`"
writer.print '| '
testSlice.importedAutoConfiguration.each {
writer.println "`${it}`"
}
}
writer.println '|==='
}
}
List<TestSlice> testSlices = createTestSlices(new Project(new File(project.build.directory, 'test-auto-config')))
writeTestSlicesTable(testSlices)
Loading…
Cancel
Save