From f46902869ff44a0542808c0e3ac2f0348b301eeb Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Tue, 21 Nov 2023 17:25:40 +0800 Subject: [PATCH] Polish LruContextCacheTests Use AssertJ's extracting() feature instead of ReflectionTestUtils.getField(). Closes gh-31640 --- .../context/cache/LruContextCacheTests.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/cache/LruContextCacheTests.java b/spring-test/src/test/java/org/springframework/test/context/cache/LruContextCacheTests.java index b92317e969..819b020f12 100644 --- a/spring-test/src/test/java/org/springframework/test/context/cache/LruContextCacheTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/cache/LruContextCacheTests.java @@ -17,16 +17,16 @@ package org.springframework.test.context.cache; import java.util.List; -import java.util.Map; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.MergedContextConfiguration; -import org.springframework.test.util.ReflectionTestUtils; import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.as; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.Mockito.mock; @@ -158,17 +158,16 @@ class LruContextCacheTests { @SuppressWarnings("unchecked") private static void assertCacheContents(DefaultContextCache cache, String... expectedNames) { - Map contextMap = - (Map) ReflectionTestUtils.getField(cache, "contextMap"); - - // @formatter:off - List actualNames = contextMap.keySet().stream() - .map(cfg -> cfg.getClasses()[0]) - .map(Class::getSimpleName) - .toList(); - // @formatter:on - - assertThat(actualNames).isEqualTo(asList(expectedNames)); + assertThat(cache).extracting("contextMap", as(InstanceOfAssertFactories.map(MergedContextConfiguration.class, ApplicationContext.class))) + .satisfies((contextMap) -> { + // @formatter:off + List actualNames = contextMap.keySet().stream() + .map(cfg -> cfg.getClasses()[0]) + .map(Class::getSimpleName) + .toList(); + // @formatter:on + assertThat(actualNames).isEqualTo(asList(expectedNames)); + }); }