Browse Source
* SPR-13998: Polish ContextCustomizer support in the TCF Introduce ContextCustomizer API in the TestContext Frameworkpull/1002/head
Sam Brannen
9 years ago
12 changed files with 376 additions and 16 deletions
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* 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.springframework.context.ConfigurableApplicationContext; |
||||
|
||||
/** |
||||
* Strategy interface for customizing {@link ConfigurableApplicationContext |
||||
* application contexts} that are created and managed by the <em>Spring |
||||
* TestContext Framework</em>. |
||||
* |
||||
* <p>Customizers are created by {@link ContextCustomizerFactory} implementations. |
||||
* |
||||
* <p>Implementations must implement correct {@code equals} and {@code hashCode} |
||||
* methods since customizers form part of the {@link MergedContextConfiguration} |
||||
* which is used as a cache key. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Sam Brannen |
||||
* @since 4.3 |
||||
* @see ContextCustomizerFactory |
||||
* @see org.springframework.test.context.support.AbstractContextLoader#customizeContext |
||||
*/ |
||||
public interface ContextCustomizer { |
||||
|
||||
/** |
||||
* Customize the supplied {@code ConfigurableApplicationContext} <em>after</em> |
||||
* bean definitions have been loaded into the context but <em>before</em> the |
||||
* context has been refreshed. |
||||
* @param context the context to customize |
||||
* @param mergedConfig the merged context configuration |
||||
*/ |
||||
void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig); |
||||
|
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* 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 java.util.List; |
||||
|
||||
/** |
||||
* Factory for creating {@link ContextCustomizer ContextCustomizers}. |
||||
* |
||||
* <p>Factories are invoked after {@link ContextLoader ContextLoaders} have |
||||
* processed context configuration attributes but before the |
||||
* {@link MergedContextConfiguration} is created. |
||||
* |
||||
* <p>By default, the Spring TestContext Framework will use the |
||||
* {@link org.springframework.core.io.support.SpringFactoriesLoader SpringFactoriesLoader} |
||||
* mechanism for loading factories configured in all {@code META-INF/spring.factories} |
||||
* files on the classpath. |
||||
* |
||||
* @author Phillip Webb |
||||
* @author Sam Brannen |
||||
* @since 4.3 |
||||
*/ |
||||
public interface ContextCustomizerFactory { |
||||
|
||||
/** |
||||
* Create a {@link ContextCustomizer} that should be used to customize a |
||||
* {@link org.springframework.context.ConfigurableApplicationContext ConfigurableApplicationContext} |
||||
* before it is refreshed. |
||||
* @param testClass the test class |
||||
* @param configAttributes the list of context configuration attributes for |
||||
* the test class, ordered <em>bottom-up</em> (i.e., as if we were traversing |
||||
* up the class hierarchy); never {@code null} or empty |
||||
* @return a {@link ContextCustomizer} or {@code null} if no customizer should |
||||
* be used |
||||
*/ |
||||
ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes); |
||||
|
||||
} |
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
/* |
||||
* 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.List; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.test.context.BootstrapWith; |
||||
import org.springframework.test.context.ContextCustomizer; |
||||
import org.springframework.test.context.ContextCustomizerFactory; |
||||
import org.springframework.test.context.junit4.ContextCustomizerSpringRunnerTests.CustomTestContextBootstrapper; |
||||
import org.springframework.test.context.support.DefaultTestContextBootstrapper; |
||||
|
||||
import static java.util.Collections.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* JUnit 4 based integration test which verifies support of |
||||
* {@link ContextCustomizerFactory} and {@link ContextCustomizer}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @author Phillip Webb |
||||
* @since 4.3 |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@BootstrapWith(CustomTestContextBootstrapper.class) |
||||
public class ContextCustomizerSpringRunnerTests { |
||||
|
||||
@Autowired String foo; |
||||
|
||||
|
||||
@Test |
||||
public void injectedBean() { |
||||
assertEquals("foo", foo); |
||||
} |
||||
|
||||
|
||||
static class CustomTestContextBootstrapper extends DefaultTestContextBootstrapper { |
||||
|
||||
@Override |
||||
protected List<ContextCustomizerFactory> getContextCustomizerFactories() { |
||||
return singletonList((testClass, configAttributes) -> |
||||
// ContextCustomizer as lambda expression:
|
||||
(context, mergedConfig) -> context.getBeanFactory().registerSingleton("foo", "foo")); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue