Introduce ContextCustomizer API in the TestContext Framework

Allow third-parties to contribute ContextCustomizers that can customize
ApplicationContexts created by the Spring TestContext Framework (TCF)
before they are refreshed.

A customizer may be provided via a ContextCustomizerFactory which is
registered with `spring.factories`. Each factory is consulted whenever
a new ApplicationContext needs to be created by the TCF. Factories may
inspect various details about the test and either return a new
ContextCustomizer or null.

ContextCustomizers are similar to ApplicationContextInitializers and
may perform any number of tasks, including bean registration, setting
of active profiles, etc.

Issue: SPR-13998
This commit is contained in:
Phillip Webb
2016-02-28 22:42:17 -08:00
committed by Sam Brannen
parent 406d06118d
commit 87a3a5cb3b
12 changed files with 392 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 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;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@@ -28,6 +29,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.support.GenericXmlContextLoader;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link MergedContextConfiguration}.
@@ -400,6 +402,36 @@ public class MergedContextConfigurationTests {
assertNotEquals(mergedConfig2, mergedConfig1);
}
@Test
public void equalsWithSameContextCustomizers() {
Set<ContextCustomizer> customizers1 = Collections.singleton(
mock(ContextCustomizer.class));
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null,
EMPTY_STRING_ARRAY, null, null, customizers1, loader, null, null);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null,
EMPTY_STRING_ARRAY, null, null, customizers1, loader, null, null);
assertEquals(mergedConfig1, mergedConfig2);
}
@Test
public void equalsWithDifferentContextCustomizers() {
Set<ContextCustomizer> customizers1 = Collections.singleton(
mock(ContextCustomizer.class));
Set<ContextCustomizer> customizers2 = Collections.singleton(
mock(ContextCustomizer.class));
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null,
EMPTY_STRING_ARRAY, null, null, customizers1, loader, null, null);
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(
getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, null,
EMPTY_STRING_ARRAY, null, null, customizers2, loader, null, null);
assertNotEquals(mergedConfig1, mergedConfig2);
assertNotEquals(mergedConfig2, mergedConfig1);
}
/**
* @since 3.2.2
*/

View File

@@ -0,0 +1,71 @@
/*
* 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;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import static org.hamcrest.CoreMatchers.*;
/**
* JUnit 4 based unit test for {@link TestContextManager}, which verifies
* ContextConfiguration attributes are defined.
*
* @author Phillip Webb
* @since 4.3
*/
public class TestContextManagerVerifyAttributesTests {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void processContextConfigurationWithMissingContextConfigAttributes() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("was unable to detect defaults, "
+ "and no ApplicationContextInitializers or ContextCustomizers were "
+ "declared for context configuration"));
new TestContextManager(MissingContextAttributes.class);
}
@Test
public void processContextConfigurationWitListener() {
new TestContextManager(WithInitializer.class);
}
@ContextConfiguration
private static class MissingContextAttributes {
}
@ContextConfiguration(initializers=ExampleInitializer.class)
private static class WithInitializer {
}
static class ExampleInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
}
}
}

View File

@@ -0,0 +1,88 @@
/*
* 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 java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.junit4.ContextCustomizerSpringRunnerTests.CustomTestContextBootstrapper;
import org.springframework.test.context.support.DefaultTestContextBootstrapper;
import static org.junit.Assert.*;
/**
* JUnit 4 based integration test which verifies support of
* {@link ContextCustomizerFactory} and {@link ContextCustomizer}.
*
* @author Phillip Webb
* @since 4.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(CustomTestContextBootstrapper.class)
@ContextConfiguration
public class ContextCustomizerSpringRunnerTests {
@Autowired
private MyBean myBean;
@Test
public void injectedMyBean() throws Exception {
assertNotNull(this.myBean);
}
public static class CustomTestContextBootstrapper
extends DefaultTestContextBootstrapper {
@Override
protected List<ContextCustomizerFactory> geContextCustomizerFactories() {
return Collections.singletonList(new ContextCustomizerFactory() {
@Override
public ContextCustomizer getContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configurationAttributes) {
return new TestContextCustomizers();
}
});
}
}
public static class TestContextCustomizers implements ContextCustomizer {
@Override
public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedContextConfiguration) {
context.getBeanFactory().registerSingleton("mybean", new MyBean());
}
}
public static class MyBean {
}
}

View File

@@ -63,7 +63,7 @@ public class BootstrapTestUtilsMergedConfigTests extends AbstractContextConfigur
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"));
+ "and no ApplicationContextInitializers or ContextCustomizers were declared for context configuration attribute"));
buildMergedContextConfiguration(MissingContextAttributesTestCase.class);
}