Skip to content

Commit d01bc41

Browse files
committed
Detect XML config files and Groovy test scripts
Update SpringApplicationContextLoader to detect xml and groovy configuration based on convention. Fixes gh-2516
1 parent 3b93a82 commit d01bc41

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ private List<ApplicationContextInitializer<?>> getInitializers(
211211
@Override
212212
public void processContextConfiguration(
213213
ContextConfigurationAttributes configAttributes) {
214+
super.processContextConfiguration(configAttributes);
214215
if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) {
215216
Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes
216217
.getDeclaringClass());
@@ -238,9 +239,14 @@ public ApplicationContext loadContext(String... locations) throws Exception {
238239
+ "does not support the loadContext(String...) method");
239240
}
240241

242+
@Override
243+
protected String[] getResourceSuffixes() {
244+
return new String[] { "-context.xml", "Context.groovy" };
245+
}
246+
241247
@Override
242248
protected String getResourceSuffix() {
243-
return "-context.xml";
249+
throw new IllegalStateException();
244250
}
245251

246252
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2015 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;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
24+
import static org.hamcrest.Matchers.equalTo;
25+
import static org.junit.Assert.assertThat;
26+
27+
/**
28+
* Tests for {@link SpringApplicationContextLoader} finding groovy config.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@SpringApplicationConfiguration
34+
public class SpringApplicationConfigurationGroovyConventionConfigurationTests {
35+
36+
@Autowired
37+
private String foo;
38+
39+
@Test
40+
public void groovyConfigLoaded() {
41+
assertThat(this.foo, equalTo("World"));
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2015 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;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
24+
import static org.hamcrest.Matchers.equalTo;
25+
import static org.junit.Assert.assertThat;
26+
27+
/**
28+
* Tests for {@link SpringApplicationContextLoader} finding XML config.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
@RunWith(SpringJUnit4ClassRunner.class)
33+
@SpringApplicationConfiguration
34+
public class SpringApplicationConfigurationXmlConventionConfigurationTests {
35+
36+
@Autowired
37+
private String foo;
38+
39+
@Test
40+
public void groovyConfigLoaded() {
41+
assertThat(this.foo, equalTo("World"));
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
beans {
2+
foo String, "World"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
7+
<bean id="foo" class="java.lang.String">
8+
<constructor-arg>
9+
<value>World</value>
10+
</constructor-arg>
11+
</bean>
12+
13+
</beans>

0 commit comments

Comments
 (0)