Allow @ContextConfiguration to be omitted

Prior to this commit, the @ContextConfiguration annotation was required
to be present even if default XML files, Groovy scripts, or
@Configuration classes were detected; however, in such cases the
@ContextConfiguration was typically declared empty and therefore
seemingly unnecessary boilerplate.

This commit permits @ContextConfiguration to be omitted whenever it can
be reasonably deduced. Consequently, integration tests such as the
following are now supported.

    @RunWith(SpringRunner.class)
    public class MyTest {

        @Autowired String myBean;

        @Test public void example() { /* ... */ }

        @Configuration
        static class Config {

            @Bean String myBean() {
                return "Hello";
            }
        }
    }

Issue: SPR-13955
This commit is contained in:
Phillip Webb
2016-02-28 23:05:00 -08:00
committed by Sam Brannen
parent 0a56667093
commit 2244461778
8 changed files with 221 additions and 94 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2002-2016 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.test.context.junit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertEquals;
/**
* JUnit 4 based integration test which verifies that {@link @ContextConfiguration}
* is optional.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class OptionalContextConfigurationSpringRunnerTests {
@Autowired
String foo;
@Test
public void contextConfigurationAnnotationIsOptional() {
assertEquals("foo", foo);
}
@Configuration
static class Config {
@Bean
String foo() {
return "foo";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,6 +16,7 @@
package org.springframework.test.context.support;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@@ -35,63 +36,62 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
* @author Sam Brannen
* @since 3.1
*/
@SuppressWarnings("unchecked")
public class BootstrapTestUtilsContextInitializerTests extends AbstractContextConfigurationUtilsTests {
@Test
public void buildMergedConfigWithLocalInitializer() {
Class<?> testClass = InitializersFoo.class;
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class };
Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(FooInitializer.class);
public void buildMergedConfigWithSingleLocalInitializer() {
Class<?> testClass = SingleInitializer.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY,
initializers(FooInitializer.class), DelegatingSmartContextLoader.class);
}
@Test
public void buildMergedConfigWithLocalInitializerAndConfigClass() {
Class<?> testClass = InitializersFoo.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(FooConfig.class),
initializers(FooInitializer.class), DelegatingSmartContextLoader.class);
}
@Test
public void buildMergedConfigWithLocalAndInheritedInitializer() {
Class<?> testClass = InitializersBar.class;
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(FooInitializer.class);
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(FooConfig.class, BarConfig.class),
initializers(FooInitializer.class, BarInitializer.class), DelegatingSmartContextLoader.class);
}
@Test
public void buildMergedConfigWithOverriddenInitializers() {
Class<?> testClass = OverriddenInitializersBar.class;
Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(FooConfig.class, BarConfig.class),
initializers(BarInitializer.class), DelegatingSmartContextLoader.class);
}
@Test
public void buildMergedConfigWithOverriddenInitializersAndClasses() {
Class<?> testClass = OverriddenInitializersAndClassesBar.class;
Class<?>[] expectedClasses = new Class<?>[] { BarConfig.class };
Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
expectedInitializerClasses.add(BarInitializer.class);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
DelegatingSmartContextLoader.class);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(BarConfig.class),
initializers(BarInitializer.class), DelegatingSmartContextLoader.class);
}
private Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializers(
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>... classes) {
return new HashSet<>(Arrays.asList(classes));
}
private Class<?>[] classes(Class<?>... classes) {
return classes;
}
@@ -109,6 +109,10 @@ public class BootstrapTestUtilsContextInitializerTests extends AbstractContextCo
}
}
@ContextConfiguration(initializers = FooInitializer.class)
private static class SingleInitializer {
}
@ContextConfiguration(classes = FooConfig.class, initializers = FooInitializer.class)
private static class InitializersFoo {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -21,15 +21,20 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.test.context.BootstrapTestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.web.WebDelegatingSmartContextLoader;
import org.springframework.test.context.web.WebMergedContextConfiguration;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Unit tests for {@link BootstrapTestUtils} involving {@link MergedContextConfiguration}.
@@ -39,12 +44,28 @@ import static org.junit.Assert.*;
*/
public class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigurationUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void buildMergedConfigWithoutAnnotation() {
public void buildImplicitMergedConfigWithoutAnnotation() {
Class<?> testClass = Enigma.class;
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null);
assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, DelegatingSmartContextLoader.class);
}
/**
* @since 4.3
*/
@Test
public void buildMergedConfigWithContextConfigurationWithoutLocationsClassesOrInitializers() {
exception.expect(IllegalStateException.class);
exception.expectMessage(startsWith("DelegatingSmartContextLoader was unable to detect defaults, "
+ "and no ApplicationContextInitializers were declared for context configuration attributes"));
buildMergedContextConfiguration(MissingContextAttributesTestCase.class);
}
@Test
@@ -200,4 +221,8 @@ public class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigur
public static class GermanShepherd extends WorkingDog {
}
@ContextConfiguration
static class MissingContextAttributesTestCase {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -55,17 +55,6 @@ public class DelegatingSmartContextLoaderTests {
// --- SmartContextLoader - processContextConfiguration() ------------------
@Test
public void processContextConfigurationWithoutLocationsAndConfigurationClassesForBogusTestClass() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(containsString("was able to detect defaults"));
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(getClass(),
EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class);
loader.processContextConfiguration(configAttributes);
}
@Test
public void processContextConfigurationWithDefaultXmlConfigGeneration() {
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(XmlTestCase.class,