Skip to content

Commit 5abc050

Browse files
committed
Support detection and with test initializers
Relax `SpringBootTestContextBootstrapper` rules so that a test can specify an `ApplicationContextInitializer` and still have `@SpringBootConfiguration` detected. Prior to this commit detection would not occur because it's possible that an initializer _could_ register configuration. This scenario is actually quite unlikely to occur, certainly less likely than wanting to use an initializer in combination with auto-detection. Fixes gh-8483
1 parent c9561f0 commit 5abc050

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/DelegatingValidator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public void validate(Object target, Errors errors, Object... validationHints) {
7171
}
7272
}
7373

74+
/**
75+
* Return the delegate validator.
76+
* @return the delegate validator
77+
*/
7478
protected final Validator getDelegate() {
7579
return this.delegate;
7680
}

spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ private boolean hasWebEnvironmentClasses() {
194194
protected Class<?>[] getOrFindConfigurationClasses(
195195
MergedContextConfiguration mergedConfig) {
196196
Class<?>[] classes = mergedConfig.getClasses();
197-
if (containsNonTestComponent(classes) || mergedConfig.hasLocations()
198-
|| !mergedConfig.getContextInitializerClasses().isEmpty()) {
197+
if (containsNonTestComponent(classes) || mergedConfig.hasLocations()) {
199198
return classes;
200199
}
201200
Class<?> found = new SpringBootConfigurationFinder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.context.bootstrap;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
24+
import org.springframework.boot.test.context.bootstrap.SpringBootTestContextBootstrapperWithInitializersTests.CustomInitializer;
25+
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.ApplicationContextInitializer;
27+
import org.springframework.context.ConfigurableApplicationContext;
28+
import org.springframework.test.context.BootstrapWith;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit4.SpringRunner;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Integration tests for {@link SpringBootTestContextBootstrapper} with and
36+
* {@link ApplicationContextInitializer}.
37+
*
38+
* @author Phillip Webb
39+
*/
40+
@RunWith(SpringRunner.class)
41+
@BootstrapWith(SpringBootTestContextBootstrapper.class)
42+
@ContextConfiguration(initializers = CustomInitializer.class)
43+
public class SpringBootTestContextBootstrapperWithInitializersTests {
44+
45+
@Autowired
46+
private ApplicationContext context;
47+
48+
@Test
49+
public void foundConfiguration() throws Exception {
50+
Object bean = this.context
51+
.getBean(SpringBootTestContextBootstrapperExampleConfig.class);
52+
assertThat(bean).isNotNull();
53+
}
54+
55+
// gh-8483
56+
57+
public static class CustomInitializer
58+
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
59+
60+
@Override
61+
public void initialize(ConfigurableApplicationContext applicationContext) {
62+
}
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)