mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '2.2.x'
Closes gh-21102
This commit is contained in:
commit
b3ccefdb57
@ -78,8 +78,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
*/
|
||||
public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
|
||||
private static final String[] NO_ARGS = new String[0];
|
||||
|
||||
@Override
|
||||
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
|
||||
Class<?>[] configClasses = config.getClasses();
|
||||
@ -145,11 +143,16 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
* empty array.
|
||||
* @param config the source context configuration
|
||||
* @return the application arguments to use
|
||||
* @deprecated since 2.2.7
|
||||
* @see SpringApplication#run(String...)
|
||||
*/
|
||||
protected String[] getArgs(MergedContextConfiguration config) {
|
||||
return MergedAnnotations.from(config.getTestClass(), SearchStrategy.TYPE_HIERARCHY).get(SpringBootTest.class)
|
||||
.getValue("args", String[].class).orElse(NO_ARGS);
|
||||
ContextCustomizer customizer = config.getContextCustomizers().stream()
|
||||
.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) {
|
||||
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizer} to track application arguments that are used in a
|
||||
* {@link SpringBootTest}. The application arguments are taken into account when
|
||||
* evaluating a {@link MergedContextConfiguration} to determine if a context can be shared
|
||||
* between tests.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer {
|
||||
|
||||
static final String[] NO_ARGS = new String[0];
|
||||
|
||||
private String[] args;
|
||||
|
||||
SpringBootTestArgsTrackingContextCustomizer(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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return (obj != null) && (getClass() == obj.getClass())
|
||||
&& Arrays.equals(this.args, ((SpringBootTestArgsTrackingContextCustomizer) obj).args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(this.args);
|
||||
}
|
||||
|
||||
}
|
@ -19,6 +19,7 @@ package org.springframework.boot.test.context;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ -40,6 +41,7 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.ContextHierarchy;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
@ -355,11 +357,12 @@ 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()));
|
||||
return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes,
|
||||
mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(),
|
||||
mergedConfig.getPropertySourceLocations(), propertySourceProperties,
|
||||
mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(),
|
||||
getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
|
||||
mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,
|
||||
mergedConfig.getContextLoader(), getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,8 +23,11 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -50,15 +53,33 @@ class SpringBootTestContextBootstrapperTests {
|
||||
buildTestContext(SpringBootTestMockWebEnvironmentAndWebAppConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() {
|
||||
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
|
||||
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
|
||||
TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class);
|
||||
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
|
||||
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
|
||||
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
|
||||
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
|
||||
TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class);
|
||||
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
|
||||
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void buildTestContext(Class<?> testClass) {
|
||||
private TestContext buildTestContext(Class<?> testClass) {
|
||||
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
|
||||
BootstrapContext bootstrapContext = mock(BootstrapContext.class);
|
||||
bootstrapper.setBootstrapContext(bootstrapContext);
|
||||
given((Class) bootstrapContext.getTestClass()).willReturn(testClass);
|
||||
CacheAwareContextLoaderDelegate contextLoaderDelegate = mock(CacheAwareContextLoaderDelegate.class);
|
||||
given(bootstrapContext.getCacheAwareContextLoaderDelegate()).willReturn(contextLoaderDelegate);
|
||||
bootstrapper.buildTestContext();
|
||||
return bootstrapper.buildTestContext();
|
||||
}
|
||||
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@ -73,4 +94,19 @@ class SpringBootTestContextBootstrapperTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(args = "--app.test=same")
|
||||
static class SpringBootTestArgsConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(args = "--app.test=same")
|
||||
static class SpringBootTestSameArgsConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(args = "--app.test=different")
|
||||
static class SpringBootTestOtherArgsConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user