Skip to content

Commit 5f7d175

Browse files
committed
Always pass test class to ActiveProfilesResolver
Prior to this commit, if @activeprofiles were used as a meta-annotation on a composed annotation, then the composed annotation's class would be passed to the ActiveProfilesResolver.resolve() method instead of the test class, which breaks the contract for ActiveProfilesResolver. This commit addresses this issue by ensuring that the actual test class is always passed to ActiveProfilesResolver.resolve(). Issue: SPR-11467
1 parent 34e90ba commit 5f7d175

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spring-test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ static String[] resolveActiveProfiles(Class<?> testClass) {
507507
final Set<String> activeProfiles = new HashSet<String>();
508508

509509
while (descriptor != null) {
510+
Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
510511
Class<?> declaringClass = descriptor.getDeclaringClass();
511512

512513
AnnotationAttributes annAttrs = descriptor.getAnnotationAttributes();
@@ -530,12 +531,12 @@ static String[] resolveActiveProfiles(Class<?> testClass) {
530531
}
531532
catch (Exception e) {
532533
String msg = String.format("Could not instantiate ActiveProfilesResolver of "
533-
+ "type [%s] for test class [%s].", resolverClass.getName(), declaringClass.getName());
534+
+ "type [%s] for test class [%s].", resolverClass.getName(), rootDeclaringClass.getName());
534535
logger.error(msg);
535536
throw new IllegalStateException(msg, e);
536537
}
537538

538-
profiles = resolver.resolve(declaringClass);
539+
profiles = resolver.resolve(rootDeclaringClass);
539540
if (profiles == null) {
540541
String msg = String.format(
541542
"ActiveProfilesResolver [%s] returned a null array of bean definition profiles.",
@@ -555,7 +556,7 @@ else if (valueDeclared) {
555556
}
556557

557558
descriptor = annAttrs.getBoolean("inheritProfiles") ? findAnnotationDescriptor(
558-
descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null;
559+
rootDeclaringClass.getSuperclass(), annotationType) : null;
559560
}
560561

561562
return StringUtils.toStringArray(activeProfiles);

spring-test/src/test/java/org/springframework/test/context/ContextLoaderUtilsActiveProfilesTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ public void resolveActiveProfilesWithResolverThatReturnsNull() {
205205
resolveActiveProfiles(NullActiveProfilesResolverTestCase.class);
206206
}
207207

208+
/**
209+
* This test verifies that the actual test class, not the composed annotation,
210+
* is passed to the resolver.
211+
*
212+
* @since 4.0.3
213+
*/
214+
@Test
215+
public void resolveActiveProfilesWithMetaAnnotationAndTestClassVerifyingResolver() {
216+
Class<TestClassVerifyingActiveProfilesResolverTestCase> testClass = TestClassVerifyingActiveProfilesResolverTestCase.class;
217+
assertResolvedProfiles(testClass, testClass.getSimpleName());
218+
}
219+
208220

209221
// -------------------------------------------------------------------------
210222

@@ -239,6 +251,12 @@ private static class Animals extends LocationsBar {
239251
boolean inheritProfiles() default false;
240252
}
241253

254+
@ActiveProfiles(resolver = TestClassVerifyingActiveProfilesResolver.class)
255+
@Retention(RetentionPolicy.RUNTIME)
256+
@Target(ElementType.TYPE)
257+
private static @interface MetaResolverConfig {
258+
}
259+
242260
@MetaAnimalsConfig
243261
private static class MetaAnimals extends MetaLocationsBar {
244262
}
@@ -282,6 +300,10 @@ private static class ConflictingResolverAndProfilesTestCase {
282300
private static class ConflictingResolverAndValueTestCase {
283301
}
284302

303+
@MetaResolverConfig
304+
private static class TestClassVerifyingActiveProfilesResolverTestCase {
305+
}
306+
285307
@ActiveProfiles(profiles = "conflict", value = "conflict")
286308
private static class ConflictingProfilesAndValueTestCase {
287309
}
@@ -322,4 +344,13 @@ public String[] resolve(Class<?> testClass) {
322344
}
323345
}
324346

347+
private static class TestClassVerifyingActiveProfilesResolver implements ActiveProfilesResolver {
348+
349+
@Override
350+
public String[] resolve(Class<?> testClass) {
351+
return testClass.isAnnotation() ? new String[] { "@" + testClass.getSimpleName() }
352+
: new String[] { testClass.getSimpleName() };
353+
}
354+
}
355+
325356
}

0 commit comments

Comments
 (0)