diff --git a/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java index 927cd9acc4..2354de61bd 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/ActiveProfilesUtils.java @@ -17,7 +17,6 @@ package org.springframework.test.context.support; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -110,15 +109,15 @@ abstract class ActiveProfilesUtils { String[] profiles = resolver.resolve(rootDeclaringClass); if (!ObjectUtils.isEmpty(profiles)) { - profileArrays.add(profiles); + // Prepend to the list so that we can later traverse "down" the hierarchy + // to ensure that we retain the top-down profile registration order + // within a test class hierarchy. + profileArrays.add(0, profiles); } descriptor = (annotation.inheritProfiles() ? descriptor.next() : null); } - // Reverse the list so that we can traverse "down" the hierarchy. - Collections.reverse(profileArrays); - Set activeProfiles = new LinkedHashSet<>(); for (String[] profiles : profileArrays) { for (String profile : profiles) {