From cf1d4638aece3d79632dec114be2853382c93f69 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 2 Aug 2022 15:12:40 +0300 Subject: [PATCH] Avoid use of deprecated ContextLoader methods in tests See gh-28905 --- ...ustomizedGenericXmlContextLoaderTests.java | 16 ++++++++++------ ...mlContextLoaderResourceLocationsTests.java | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java b/spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java index aeb340d2fe..6efe5fc183 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/support/CustomizedGenericXmlContextLoaderTests.java @@ -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; 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(); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java b/spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java index d0e7e12642..6b9e8e34ec 100644 --- a/spring-test/src/test/java/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests.java +++ b/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.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; /** * 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 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 { 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 contextConfigurationLocationsData() {