diff --git a/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index bb22a5a39f3..b2d98ea7681 100644 --- a/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -507,6 +507,7 @@ abstract class ContextLoaderUtils { final Set activeProfiles = new HashSet(); while (descriptor != null) { + Class rootDeclaringClass = descriptor.getRootDeclaringClass(); Class declaringClass = descriptor.getDeclaringClass(); AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes(); @@ -530,12 +531,12 @@ abstract class ContextLoaderUtils { } catch (Exception e) { String msg = String.format("Could not instantiate ActiveProfilesResolver of " - + "type [%s] for test class [%s].", resolverClass.getName(), declaringClass.getName()); + + "type [%s] for test class [%s].", resolverClass.getName(), rootDeclaringClass.getName()); logger.error(msg); throw new IllegalStateException(msg, e); } - profiles = resolver.resolve(declaringClass); + profiles = resolver.resolve(rootDeclaringClass); if (profiles == null) { String msg = String.format( "ActiveProfilesResolver [%s] returned a null array of bean definition profiles.", @@ -555,7 +556,7 @@ abstract class ContextLoaderUtils { } descriptor = annAttrs.getBoolean("inheritProfiles") ? findAnnotationDescriptor( - descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null; + rootDeclaringClass.getSuperclass(), annotationType) : null; } return StringUtils.toStringArray(activeProfiles); diff --git a/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java b/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java index af81a22d16e..781b2e2772f 100644 --- a/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java @@ -205,6 +205,18 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader resolveActiveProfiles(NullActiveProfilesResolverTestCase.class); } + /** + * This test verifies that the actual test class, not the composed annotation, + * is passed to the resolver. + * + * @since 4.0.3 + */ + @Test + public void resolveActiveProfilesWithMetaAnnotationAndTestClassVerifyingResolver() { + Class testClass = TestClassVerifyingActiveProfilesResolverTestCase.class; + assertResolvedProfiles(testClass, testClass.getSimpleName()); + } + // ------------------------------------------------------------------------- @@ -239,6 +251,12 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader boolean inheritProfiles() default false; } + @ActiveProfiles(resolver = TestClassVerifyingActiveProfilesResolver.class) + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + private static @interface MetaResolverConfig { + } + @MetaAnimalsConfig private static class MetaAnimals extends MetaLocationsBar { } @@ -282,6 +300,10 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader private static class ConflictingResolverAndValueTestCase { } + @MetaResolverConfig + private static class TestClassVerifyingActiveProfilesResolverTestCase { + } + @ActiveProfiles(profiles = "conflict", value = "conflict") private static class ConflictingProfilesAndValueTestCase { } @@ -322,4 +344,13 @@ public class ContextLoaderUtilsActiveProfilesTests extends AbstractContextLoader } } + private static class TestClassVerifyingActiveProfilesResolver implements ActiveProfilesResolver { + + @Override + public String[] resolve(Class testClass) { + return testClass.isAnnotation() ? new String[] { "@" + testClass.getSimpleName() } + : new String[] { testClass.getSimpleName() }; + } + } + }