Merge branch '2.2.x'

pull/21110/head
Phillip Webb 5 years ago
commit b73d5038d5

@ -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) {

@ -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 {};

@ -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<ContextCustomizer> customizers) {
for (ContextCustomizer customizer : customizers) {
if (customizer instanceof SpringBootTestArgs) {
return ((SpringBootTestArgs) customizer).args;
}
}
return NO_ARGS;
}
}

@ -358,7 +358,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
protected final MergedContextConfiguration createModifiedConfig(MergedContextConfiguration mergedConfig,
Class<?>[] classes, String[] propertySourceProperties) {
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,
mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(),
mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,

Loading…
Cancel
Save