Merge branch '2.2.x'

This commit is contained in:
Phillip Webb 2020-04-23 14:35:08 -07:00
commit b73d5038d5
4 changed files with 24 additions and 17 deletions

View File

@ -146,13 +146,9 @@ public class SpringBootContextLoader extends AbstractContextLoader {
* @deprecated since 2.2.7 * @deprecated since 2.2.7
* @see SpringApplication#run(String...) * @see SpringApplication#run(String...)
*/ */
@Deprecated
protected String[] getArgs(MergedContextConfiguration config) { protected String[] getArgs(MergedContextConfiguration config) {
ContextCustomizer customizer = config.getContextCustomizers().stream() return SpringBootTestArgs.get(config.getContextCustomizers());
.filter((c) -> c instanceof SpringBootTestArgsTrackingContextCustomizer).findFirst().orElse(null);
if (customizer != null) {
return ((SpringBootTestArgsTrackingContextCustomizer) customizer).getArgs();
}
return SpringBootTestArgsTrackingContextCustomizer.NO_ARGS;
} }
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {

View File

@ -101,6 +101,7 @@ public @interface SpringBootTest {
* @return the application arguments to pass to the application under test. * @return the application arguments to pass to the application under test.
* @see ApplicationArguments * @see ApplicationArguments
* @see SpringApplication#run(String...) * @see SpringApplication#run(String...)
* @since 2.2.0
*/ */
String[] args() default {}; String[] args() default {};

View File

@ -17,6 +17,7 @@
package org.springframework.boot.test.context; package org.springframework.boot.test.context;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations;
@ -31,26 +32,21 @@ import org.springframework.test.context.MergedContextConfiguration;
* *
* @author Madhura Bhave * @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) this.args = MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.get(SpringBootTest.class).getValue("args", String[].class).orElse(NO_ARGS); .get(SpringBootTest.class).getValue("args", String[].class).orElse(NO_ARGS);
} }
@Override @Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
} }
/**
* Return the application arguments that are tracked by this customizer.
* @return the args
*/
String[] getArgs() { String[] getArgs() {
return this.args; return this.args;
} }
@ -58,7 +54,7 @@ class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return (obj != null) && (getClass() == obj.getClass()) return (obj != null) && (getClass() == obj.getClass())
&& Arrays.equals(this.args, ((SpringBootTestArgsTrackingContextCustomizer) obj).args); && Arrays.equals(this.args, ((SpringBootTestArgs) obj).args);
} }
@Override @Override
@ -66,4 +62,18 @@ class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer {
return Arrays.hashCode(this.args); 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<ContextCustomizer> customizers) {
for (ContextCustomizer customizer : customizers) {
if (customizer instanceof SpringBootTestArgs) {
return ((SpringBootTestArgs) customizer).args;
}
}
return NO_ARGS;
}
} }

View File

@ -358,7 +358,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
protected final MergedContextConfiguration createModifiedConfig(MergedContextConfiguration mergedConfig, protected final MergedContextConfiguration createModifiedConfig(MergedContextConfiguration mergedConfig,
Class<?>[] classes, String[] propertySourceProperties) { Class<?>[] classes, String[] propertySourceProperties) {
Set<ContextCustomizer> contextCustomizers = new LinkedHashSet<>(mergedConfig.getContextCustomizers()); Set<ContextCustomizer> 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, return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes,
mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(), mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(),
mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers, mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,