Detect XML config files and Groovy test scripts

Update SpringApplicationContextLoader to detect xml and groovy
configuration based on convention.

Fixes gh-2516
pull/4042/head
Phillip Webb 9 years ago
parent 3b93a82dd6
commit d01bc41e1e

@ -211,6 +211,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
@Override @Override
public void processContextConfiguration( public void processContextConfiguration(
ContextConfigurationAttributes configAttributes) { ContextConfigurationAttributes configAttributes) {
super.processContextConfiguration(configAttributes);
if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) { if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) {
Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes
.getDeclaringClass()); .getDeclaringClass());
@ -238,9 +239,14 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
+ "does not support the loadContext(String...) method"); + "does not support the loadContext(String...) method");
} }
@Override
protected String[] getResourceSuffixes() {
return new String[] { "-context.xml", "Context.groovy" };
}
@Override @Override
protected String getResourceSuffix() { protected String getResourceSuffix() {
return "-context.xml"; throw new IllegalStateException();
} }
/** /**

@ -0,0 +1,44 @@
/*
* Copyright 2012-2015 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;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} finding groovy config.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
public class SpringApplicationConfigurationGroovyConventionConfigurationTests {
@Autowired
private String foo;
@Test
public void groovyConfigLoaded() {
assertThat(this.foo, equalTo("World"));
}
}

@ -0,0 +1,44 @@
/*
* Copyright 2012-2015 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;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link SpringApplicationContextLoader} finding XML config.
*
* @author Phillip Webb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
public class SpringApplicationConfigurationXmlConventionConfigurationTests {
@Autowired
private String foo;
@Test
public void groovyConfigLoaded() {
assertThat(this.foo, equalTo("World"));
}
}

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="foo" class="java.lang.String">
<constructor-arg>
<value>World</value>
</constructor-arg>
</bean>
</beans>
Loading…
Cancel
Save