Skip to content

Commit 6d934bb

Browse files
committed
Add spring-boot-test-autoconfigure module
Add a new test module to deal with auto-configuration in tests. See spring-projectsgh-4901
1 parent fb70a56 commit 6d934bb

File tree

14 files changed

+482
-0
lines changed

14 files changed

+482
-0
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<module>spring-boot</module>
8585
<module>spring-boot-test</module>
8686
<module>spring-boot-autoconfigure</module>
87+
<module>spring-boot-test-autoconfigure</module>
8788
<module>spring-boot-actuator</module>
8889
<module>spring-boot-devtools</module>
8990
<module>spring-boot-docs</module>

spring-boot-dependencies/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@
193193
<artifactId>spring-boot-test</artifactId>
194194
<version>1.4.0.BUILD-SNAPSHOT</version>
195195
</dependency>
196+
<dependency>
197+
<groupId>org.springframework.boot</groupId>
198+
<artifactId>spring-boot-test-autoconfigure</artifactId>
199+
<version>1.4.0.BUILD-SNAPSHOT</version>
200+
</dependency>
196201
<dependency>
197202
<groupId>org.springframework.boot</groupId>
198203
<artifactId>spring-boot-test</artifactId>

spring-boot-full-build/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<module>../spring-boot</module>
5252
<module>../spring-boot-test</module>
5353
<module>../spring-boot-autoconfigure</module>
54+
<module>../spring-boot-test-autoconfigure</module>
5455
<module>../spring-boot-actuator</module>
5556
<module>../spring-boot-actuator-docs</module>
5657
<module>../spring-boot-devtools</module>

spring-boot-starters/spring-boot-starter-test/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<groupId>org.springframework.boot</groupId>
2323
<artifactId>spring-boot-test</artifactId>
2424
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-test-autoconfigure</artifactId>
28+
</dependency>
2529
<dependency>
2630
<groupId>com.jayway.jsonpath</groupId>
2731
<artifactId>json-path</artifactId>
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-parent</artifactId>
7+
<version>1.4.0.BUILD-SNAPSHOT</version>
8+
<relativePath>../spring-boot-parent</relativePath>
9+
</parent>
10+
<artifactId>spring-boot-test-autoconfigure</artifactId>
11+
<name>Spring Boot Test Auto-Configure</name>
12+
<description>Spring Boot Test Auto-Configure</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<!-- Compile -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-test</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-autoconfigure</artifactId>
30+
</dependency>
31+
<!-- Optional -->
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-test</artifactId>
35+
<optional>true</optional>
36+
</dependency>
37+
<!-- Test -->
38+
<dependency>
39+
<groupId>org.aspectj</groupId>
40+
<artifactId>aspectjrt</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.aspectj</groupId>
45+
<artifactId>aspectjweaver</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>ch.qos.logback</groupId>
50+
<artifactId>logback-classic</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2016 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;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
28+
/**
29+
* Annotation that can be used to override
30+
* {@link EnableAutoConfiguration @EnableAutoConfiguration}. Often used in combination
31+
* with {@link ImportAutoConfiguration} to limit the auto-configutation classes that are
32+
* loaded.
33+
*
34+
* @author Phillip Webb
35+
* @since 1.4.0
36+
* @see EnableAutoConfiguration#ENABLED_OVERRIDE_PROPERTY
37+
*/
38+
@Target(ElementType.TYPE)
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
public @interface OverrideAutoConfiguration {
42+
43+
/**
44+
* The value of the {@link EnableAutoConfiguration#ENABLED_OVERRIDE_PROPERTY enabled
45+
* property override}.
46+
*/
47+
boolean enabled();
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2012-2016 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;
18+
19+
import java.util.List;
20+
21+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
22+
import org.springframework.boot.test.util.EnvironmentTestUtils;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.core.annotation.AnnotatedElementUtils;
25+
import org.springframework.test.context.ContextConfigurationAttributes;
26+
import org.springframework.test.context.ContextCustomizer;
27+
import org.springframework.test.context.ContextCustomizerFactory;
28+
import org.springframework.test.context.MergedContextConfiguration;
29+
30+
/**
31+
* {@link ContextCustomizerFactory} to support
32+
* {@link OverrideAutoConfiguration @OverrideAutoConfiguration}.
33+
*
34+
* @author Phillip Webb
35+
*/
36+
class OverrideAutoConfigurationContextCustomizerFactory
37+
implements ContextCustomizerFactory {
38+
39+
@Override
40+
public ContextCustomizer createContextCustomizer(Class<?> testClass,
41+
List<ContextConfigurationAttributes> configurationAttributes) {
42+
OverrideAutoConfiguration annotation = AnnotatedElementUtils
43+
.findMergedAnnotation(testClass, OverrideAutoConfiguration.class);
44+
if (annotation != null && !annotation.enabled()) {
45+
return new DisableAutoConfigurationContextCustomizer();
46+
}
47+
return null;
48+
}
49+
50+
/**
51+
* {@link ContextCustomizer} to disable full auto-configuration.
52+
*/
53+
private static class DisableAutoConfigurationContextCustomizer
54+
implements ContextCustomizer {
55+
56+
@Override
57+
public void customizeContext(ConfigurableApplicationContext context,
58+
MergedContextConfiguration mergedConfig) {
59+
EnvironmentTestUtils.addEnvironment(context,
60+
EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY + "=false");
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return getClass().hashCode();
66+
}
67+
68+
@Override
69+
public boolean equals(Object obj) {
70+
return (obj != null && obj.getClass() == getClass());
71+
}
72+
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2016 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+
/**
18+
* Test auto-configuration support.
19+
*/
20+
package org.springframework.boot.test.autoconfigure;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Spring Test ContextCustomizerFactories
2+
org.springframework.test.context.ContextCustomizerFactory=\
3+
org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory
4+
5+
# Test Execution Listeners
6+
org.springframework.test.context.TestExecutionListener=\
7+
org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2016 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;
18+
19+
import org.springframework.boot.SpringBootConfiguration;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* Example {@link SpringBootApplication @SpringBootApplication} for use with
25+
* {@link OverrideAutoConfiguration} tests.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
@SpringBootConfiguration
30+
@EnableAutoConfiguration
31+
public class ExampleSpringBootApplication {
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-2016 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;
18+
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
21+
/**
22+
* Example {@link TestConfiguration @TestConfiguration} for
23+
* {@link OverrideAutoConfiguration} tests.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
@TestConfiguration
28+
public class ExampleTestConfig {
29+
30+
}

0 commit comments

Comments
 (0)