Browse Source

Avoid use of deprecated ContextLoader methods in tests

See gh-28905
pull/28912/head
Sam Brannen 2 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 @@ @@ -16,9 +16,12 @@
package org.springframework.test.context.support;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,21 +40,22 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -37,21 +40,22 @@ import static org.assertj.core.api.Assertions.assertThat;
class CustomizedGenericXmlContextLoaderTests {
@Test
@SuppressWarnings("deprecation")
void customizeContext() throws Exception {
StringBuilder builder = new StringBuilder();
String expectedContents = "customizeContext() was called";
AtomicBoolean customizeInvoked = new AtomicBoolean(false);
GenericXmlContextLoader customLoader = new GenericXmlContextLoader() {
@Override
protected void customizeContext(GenericApplicationContext context) {
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; @@ -24,9 +24,9 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.core.annotation.AnnotationUtils;
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 static org.assertj.core.api.Assertions.assertThat;
@ -35,7 +35,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments; @@ -35,7 +35,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
* Unit test which verifies proper
* {@link ContextLoader#processLocations(Class, String...) processing} of
* {@linkplain SmartContextLoader#processContextConfiguration processing} of
* {@code resource locations} by a {@link GenericXmlContextLoader}
* configured via {@link ContextConfiguration @ContextConfiguration}.
* Specifically, this test addresses the issues raised in <a
@ -55,10 +55,12 @@ class GenericXmlContextLoaderResourceLocationsTests { @@ -55,10 +55,12 @@ class GenericXmlContextLoaderResourceLocationsTests {
@MethodSource("contextConfigurationLocationsData")
void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLocations) throws Exception {
ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
ContextLoader contextLoader = new GenericXmlContextLoader();
String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
@SuppressWarnings("deprecation")
String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
String[] configuredLocations = contextConfig.value();
ContextConfigurationAttributes configAttributes =
new ContextConfigurationAttributes(testClass, configuredLocations, null, false, null, false, GenericXmlContextLoader.class);
GenericXmlContextLoader contextLoader = new GenericXmlContextLoader();
contextLoader.processContextConfiguration(configAttributes);
String[] processedLocations = configAttributes.getLocations();
if (logger.isDebugEnabled()) {
logger.debug("----------------------------------------------------------------------");
@ -67,7 +69,8 @@ class GenericXmlContextLoaderResourceLocationsTests { @@ -67,7 +69,8 @@ class GenericXmlContextLoaderResourceLocationsTests {
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() {

Loading…
Cancel
Save