Browse Source

Avoid use of deprecated ContextLoader methods in tests

See gh-28905
pull/28912/head
Sam Brannen 4 years ago
parent
commit
cf1d4638ae
  1. 16
      spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java
  2. 19
      spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java

16
spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java

@ -16,9 +16,12 @@
package org.springframework.test.context.support; package org.springframework.test.context.support;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -37,21 +40,22 @@ import static org.assertj.core.api.Assertions.assertThat;
class CustomizedGenericXmlContextLoaderTests { class CustomizedGenericXmlContextLoaderTests {
@Test @Test
@SuppressWarnings("deprecation")
void customizeContext() throws Exception { void customizeContext() throws Exception {
StringBuilder builder = new StringBuilder(); AtomicBoolean customizeInvoked = new AtomicBoolean(false);
String expectedContents = "customizeContext() was called";
GenericXmlContextLoader customLoader = new GenericXmlContextLoader() { GenericXmlContextLoader customLoader = new GenericXmlContextLoader() {
@Override @Override
protected void customizeContext(GenericApplicationContext context) { protected void customizeContext(GenericApplicationContext context) {
assertThat(context.isActive()).as("The context should not yet have been refreshed.").isFalse(); assertThat(context.isActive()).as("The context should not yet have been refreshed.").isFalse();
builder.append(expectedContents); customizeInvoked.set(true);
} }
}; };
customLoader.loadContext("classpath:/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests-context.xml");
assertThat(builder).asString().as("customizeContext() should have been called.").isEqualTo(expectedContents); MergedContextConfiguration mergedConfig =
new MergedContextConfiguration(getClass(), null, null, null, null);
customLoader.loadContext(mergedConfig);
assertThat(customizeInvoked).as("customizeContext() should have been invoked").isTrue();
} }
} }

19
spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java

@ -24,9 +24,9 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader; import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -35,7 +35,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments;
/** /**
* Unit test which verifies proper * Unit test which verifies proper
* {@link ContextLoader#processLocations(Class, String...) processing} of * {@linkplain SmartContextLoader#processContextConfiguration processing} of
* {@code resource locations} by a {@link GenericXmlContextLoader} * {@code resource locations} by a {@link GenericXmlContextLoader}
* configured via {@link ContextConfiguration @ContextConfiguration}. * configured via {@link ContextConfiguration @ContextConfiguration}.
* Specifically, this test addresses the issues raised in <a * Specifically, this test addresses the issues raised in <a
@ -55,10 +55,12 @@ class GenericXmlContextLoaderResourceLocationsTests {
@MethodSource("contextConfigurationLocationsData") @MethodSource("contextConfigurationLocationsData")
void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLocations) throws Exception { void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLocations) throws Exception {
ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class); ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
ContextLoader contextLoader = new GenericXmlContextLoader(); String[] configuredLocations = contextConfig.value();
String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig); ContextConfigurationAttributes configAttributes =
@SuppressWarnings("deprecation") new ContextConfigurationAttributes(testClass, configuredLocations, null, false, null, false, GenericXmlContextLoader.class);
String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations); GenericXmlContextLoader contextLoader = new GenericXmlContextLoader();
contextLoader.processContextConfiguration(configAttributes);
String[] processedLocations = configAttributes.getLocations();
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("----------------------------------------------------------------------"); logger.debug("----------------------------------------------------------------------");
@ -67,7 +69,8 @@ class GenericXmlContextLoaderResourceLocationsTests {
logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations)); logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations));
} }
assertThat(processedLocations).as("Verifying locations for test [" + testClass + "].").isEqualTo(expectedLocations); assertThat(processedLocations).as("locations for test class [" + testClass.getName() + "]")
.isEqualTo(expectedLocations);
} }
static Stream<Arguments> contextConfigurationLocationsData() { static Stream<Arguments> contextConfigurationLocationsData() {

Loading…
Cancel
Save