diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index 34a1a5d624..8b2be553c2 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -146,13 +146,9 @@ public class SpringBootContextLoader extends AbstractContextLoader { * @deprecated since 2.2.7 * @see SpringApplication#run(String...) */ + @Deprecated protected String[] getArgs(MergedContextConfiguration config) { - ContextCustomizer customizer = config.getContextCustomizers().stream() - .filter((c) -> c instanceof SpringBootTestArgsTrackingContextCustomizer).findFirst().orElse(null); - if (customizer != null) { - return ((SpringBootTestArgsTrackingContextCustomizer) customizer).getArgs(); - } - return SpringBootTestArgsTrackingContextCustomizer.NO_ARGS; + return SpringBootTestArgs.get(config.getContextCustomizers()); } private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java index 4ac2071f76..02d61007c0 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java @@ -101,6 +101,7 @@ public @interface SpringBootTest { * @return the application arguments to pass to the application under test. * @see ApplicationArguments * @see SpringApplication#run(String...) + * @since 2.2.0 */ String[] args() default {}; diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgsTrackingContextCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgs.java similarity index 72% rename from spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgsTrackingContextCustomizer.java rename to spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgs.java index b1486bb7e4..750754cb25 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgsTrackingContextCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestArgs.java @@ -17,6 +17,7 @@ package org.springframework.boot.test.context; import java.util.Arrays; +import java.util.Set; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.annotation.MergedAnnotations; @@ -31,26 +32,21 @@ import org.springframework.test.context.MergedContextConfiguration; * * @author Madhura Bhave */ -class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer { +class SpringBootTestArgs implements ContextCustomizer { - static final String[] NO_ARGS = new String[0]; + private static final String[] NO_ARGS = new String[0]; - private String[] args; + private final String[] args; - SpringBootTestArgsTrackingContextCustomizer(Class testClass) { + SpringBootTestArgs(Class testClass) { this.args = MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY) .get(SpringBootTest.class).getValue("args", String[].class).orElse(NO_ARGS); } @Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { - } - /** - * Return the application arguments that are tracked by this customizer. - * @return the args - */ String[] getArgs() { return this.args; } @@ -58,7 +54,7 @@ class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer { @Override public boolean equals(Object obj) { return (obj != null) && (getClass() == obj.getClass()) - && Arrays.equals(this.args, ((SpringBootTestArgsTrackingContextCustomizer) obj).args); + && Arrays.equals(this.args, ((SpringBootTestArgs) obj).args); } @Override @@ -66,4 +62,18 @@ class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer { return Arrays.hashCode(this.args); } + /** + * Return the application arguments from the given customizers. + * @param customizers the customizers to check + * @return the application args or an empty array + */ + static String[] get(Set customizers) { + for (ContextCustomizer customizer : customizers) { + if (customizer instanceof SpringBootTestArgs) { + return ((SpringBootTestArgs) customizer).args; + } + } + return NO_ARGS; + } + } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java index ef5a566b36..b8b80cbb37 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java @@ -358,7 +358,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr protected final MergedContextConfiguration createModifiedConfig(MergedContextConfiguration mergedConfig, Class[] classes, String[] propertySourceProperties) { Set contextCustomizers = new LinkedHashSet<>(mergedConfig.getContextCustomizers()); - contextCustomizers.add(new SpringBootTestArgsTrackingContextCustomizer(mergedConfig.getTestClass())); + contextCustomizers.add(new SpringBootTestArgs(mergedConfig.getTestClass())); return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes, mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(), mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,