Polish ContextCustomizer support in the TCF
Issue: SPR-13998
This commit is contained in:
@@ -39,6 +39,7 @@ import static org.mockito.Mockito.*;
|
||||
* {@link org.springframework.test.context.cache.ContextCache ContextCache}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Phillip Webb
|
||||
* @since 3.1
|
||||
*/
|
||||
public class MergedContextConfigurationTests {
|
||||
@@ -402,32 +403,31 @@ public class MergedContextConfigurationTests {
|
||||
assertNotEquals(mergedConfig2, mergedConfig1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@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);
|
||||
Set<ContextCustomizer> customizers = Collections.singleton(mock(ContextCustomizer.class));
|
||||
MergedContextConfiguration mergedConfig1 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, null, null, customizers, loader, null, null);
|
||||
MergedContextConfiguration mergedConfig2 = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, null, null, customizers, loader, null, null);
|
||||
assertEquals(mergedConfig1, mergedConfig2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.3
|
||||
*/
|
||||
@Test
|
||||
public void equalsWithDifferentContextCustomizers() {
|
||||
Set<ContextCustomizer> customizers1 = Collections.singleton(
|
||||
mock(ContextCustomizer.class));
|
||||
Set<ContextCustomizer> customizers2 = Collections.singleton(
|
||||
mock(ContextCustomizer.class));
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* 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) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -16,73 +16,50 @@
|
||||
|
||||
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 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(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@BootstrapWith(CustomTestContextBootstrapper.class)
|
||||
@ContextConfiguration
|
||||
public class ContextCustomizerSpringRunnerTests {
|
||||
|
||||
@Autowired
|
||||
private MyBean myBean;
|
||||
@Autowired String foo;
|
||||
|
||||
|
||||
@Test
|
||||
public void injectedMyBean() throws Exception {
|
||||
assertNotNull(this.myBean);
|
||||
public void injectedBean() {
|
||||
assertEquals("foo", foo);
|
||||
}
|
||||
|
||||
public static class CustomTestContextBootstrapper
|
||||
extends DefaultTestContextBootstrapper {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
});
|
||||
protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
|
||||
return singletonList((testClass, configAttributes) ->
|
||||
// ContextCustomizer as lambda expression:
|
||||
(context, mergedConfig) -> context.getBeanFactory().registerSingleton("foo", "foo"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class TestContextCustomizers implements ContextCustomizer {
|
||||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
context.getBeanFactory().registerSingleton("mybean", new MyBean());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class MyBean {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 or ContextCustomizers were declared for context configuration attribute"));
|
||||
+ "and no ApplicationContextInitializers or ContextCustomizers were declared for context configuration attributes"));
|
||||
|
||||
buildMergedContextConfiguration(MissingContextAttributesTestCase.class);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user