diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java index cd85b114aa6..e760496f43c 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ContextLoader.java @@ -154,14 +154,16 @@ public class ContextLoader { } /** - * Add the specified auto-configuration at the beginning so that it is - * applied before any other existing auto-configurations, but after any - * user configuration. - * @param autoConfiguration the auto-configuration to add + * Add the specified auto-configurations at the beginning (in that order) so that it + * is applied before any other existing auto-configurations, but after any + * user configuration. If {@code A} and {@code B} are specified, {@code A} will + * be processed, then {@code B} and finally the rest of the existing + * auto-configuration. + * @param autoConfigurations the auto-configuration to add * @return this instance */ - public ContextLoader autoConfigFirst(Class autoConfiguration) { - this.autoConfigurations.addFirst(autoConfiguration); + public ContextLoader autoConfigFirst(Class... autoConfigurations) { + this.autoConfigurations.addAll(0, Arrays.asList(autoConfigurations)); return this; } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java index ab1b2b952af..087caad7aa5 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/rule/ContextLoaderTests.java @@ -145,6 +145,16 @@ public class ContextLoaderTests { assertThat(context.getBean("a")).isEqualTo("a")); } + @Test + public void autoConfigureFirstWithSeveralConfigsIsAppliedProperly() { + this.contextLoader.autoConfig(ConfigA.class, ConfigB.class) + .autoConfigFirst(AutoConfigA.class, AutoConfigB.class) + .load(context -> { + assertThat(context.getBean("a")).isEqualTo("a"); + assertThat(context.getBean("b")).isEqualTo(1); + }); + } + @Test public void autoConfigurationIsAdditive() { this.contextLoader.autoConfig(AutoConfigA.class)