Update SpringApplicationContextLoader for Spring 4.0.3

Spring 4.0.3 is not released yet, but we can anticipate the
change. With this commit SpringApplicationContextLoader can discover
default config (nested) classes if none are provided. It should
just work when Spring is upgraded.

Fixes gh-395
pull/408/head
Dave Syer 11 years ago
parent cfba5e8361
commit 26ebc98b70

@ -29,7 +29,9 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.web.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.core.SpringVersion;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.support.AbstractContextLoader;
@ -77,6 +79,16 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
return application.run();
}
@Override
public void processContextConfiguration(
ContextConfigurationAttributes configAttributes) {
if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) {
Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes
.getDeclaringClass());
configAttributes.setClasses(defaultConfigClasses);
}
}
/**
* Builds new {@link org.springframework.boot.SpringApplication} instance. You can
* override this method to add custom behaviour
@ -92,8 +104,9 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
if (sources.isEmpty()) {
throw new IllegalStateException(
"No configuration resources found (use classes= or locations= in @SpringApplicationConfiguration). "
+ "Default configuration detection is not supported with this loader (see SPR-11455 for details)");
"No configuration classes or locations found in @SpringApplicationConfiguration. "
+ "For default configuration detection to work you need Spring 4.0.3 or better (found "
+ SpringVersion.getVersion() + ").");
}
return sources;
}

@ -16,7 +16,6 @@
package org.springframework.boot;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@ -29,9 +28,9 @@ import org.springframework.boot.test.SpringApplicationConfigurationJmxTests;
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ SpringApplicationConfigurationDefaultConfigurationTests.class,
SpringApplicationConfigurationJmxTests.class })
@Ignore
@SuiteClasses({ SpringApplicationConfigurationJmxTests.class,
SpringApplicationConfigurationDefaultConfigurationTests.class })
// @Ignore
public class AdhocTestSuite {
}

@ -33,7 +33,7 @@ import static org.junit.Assert.assertNotNull;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@Ignore("Ignore until gh-380, SPR-11455 can be resolved")
@Ignore("Ignore until SPR-11455 is resolved (Spring 4.0.3)")
public class SpringApplicationConfigurationDefaultConfigurationTests {
@Autowired

Loading…
Cancel
Save