Skip to content

Commit cf1d463

Browse files
committed
Avoid use of deprecated ContextLoader methods in tests
See gh-28905
1 parent 72548f7 commit cf1d463

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package org.springframework.test.context.support;
1818

19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
1921
import org.junit.jupiter.api.Test;
2022

2123
import org.springframework.context.support.GenericApplicationContext;
24+
import org.springframework.test.context.MergedContextConfiguration;
2225

2326
import static org.assertj.core.api.Assertions.assertThat;
2427

@@ -37,21 +40,22 @@
3740
class CustomizedGenericXmlContextLoaderTests {
3841

3942
@Test
40-
@SuppressWarnings("deprecation")
4143
void customizeContext() throws Exception {
42-
StringBuilder builder = new StringBuilder();
43-
String expectedContents = "customizeContext() was called";
44+
AtomicBoolean customizeInvoked = new AtomicBoolean(false);
4445

4546
GenericXmlContextLoader customLoader = new GenericXmlContextLoader() {
4647
@Override
4748
protected void customizeContext(GenericApplicationContext context) {
4849
assertThat(context.isActive()).as("The context should not yet have been refreshed.").isFalse();
49-
builder.append(expectedContents);
50+
customizeInvoked.set(true);
5051
}
5152
};
52-
customLoader.loadContext("classpath:/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests-context.xml");
5353

54-
assertThat(builder).asString().as("customizeContext() should have been called.").isEqualTo(expectedContents);
54+
MergedContextConfiguration mergedConfig =
55+
new MergedContextConfiguration(getClass(), null, null, null, null);
56+
customLoader.loadContext(mergedConfig);
57+
58+
assertThat(customizeInvoked).as("customizeContext() should have been invoked").isTrue();
5559
}
5660

5761
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.junit.jupiter.params.provider.Arguments;
2525
import org.junit.jupiter.params.provider.MethodSource;
2626

27-
import org.springframework.core.annotation.AnnotationUtils;
2827
import org.springframework.test.context.ContextConfiguration;
29-
import org.springframework.test.context.ContextLoader;
28+
import org.springframework.test.context.ContextConfigurationAttributes;
29+
import org.springframework.test.context.SmartContextLoader;
3030
import org.springframework.util.ObjectUtils;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,7 +35,7 @@
3535

3636
/**
3737
* Unit test which verifies proper
38-
* {@link ContextLoader#processLocations(Class, String...) processing} of
38+
* {@linkplain SmartContextLoader#processContextConfiguration processing} of
3939
* {@code resource locations} by a {@link GenericXmlContextLoader}
4040
* configured via {@link ContextConfiguration @ContextConfiguration}.
4141
* Specifically, this test addresses the issues raised in <a
@@ -55,10 +55,12 @@ class GenericXmlContextLoaderResourceLocationsTests {
5555
@MethodSource("contextConfigurationLocationsData")
5656
void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLocations) throws Exception {
5757
ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
58-
ContextLoader contextLoader = new GenericXmlContextLoader();
59-
String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
60-
@SuppressWarnings("deprecation")
61-
String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
58+
String[] configuredLocations = contextConfig.value();
59+
ContextConfigurationAttributes configAttributes =
60+
new ContextConfigurationAttributes(testClass, configuredLocations, null, false, null, false, GenericXmlContextLoader.class);
61+
GenericXmlContextLoader contextLoader = new GenericXmlContextLoader();
62+
contextLoader.processContextConfiguration(configAttributes);
63+
String[] processedLocations = configAttributes.getLocations();
6264

6365
if (logger.isDebugEnabled()) {
6466
logger.debug("----------------------------------------------------------------------");
@@ -67,7 +69,8 @@ void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLo
6769
logger.debug("Processed locations: " + ObjectUtils.nullSafeToString(processedLocations));
6870
}
6971

70-
assertThat(processedLocations).as("Verifying locations for test [" + testClass + "].").isEqualTo(expectedLocations);
72+
assertThat(processedLocations).as("locations for test class [" + testClass.getName() + "]")
73+
.isEqualTo(expectedLocations);
7174
}
7275

7376
static Stream<Arguments> contextConfigurationLocationsData() {

0 commit comments

Comments
 (0)