Skip to content

Commit

Permalink
@SpringBootTest classes with different args shouldn't share a context
Browse files Browse the repository at this point in the history
Fixes gh-20866
  • Loading branch information
mbhave committed Apr 23, 2020
1 parent 8ac3ab5 commit 4dc9bbe
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 9 deletions.
Expand Up @@ -82,8 +82,6 @@
*/
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();
Expand Down Expand Up @@ -149,11 +147,16 @@ protected ConfigurableEnvironment getEnvironment() {
* 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) {
Expand Down
@@ -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);
}

}
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Expand All @@ -40,6 +41,7 @@
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;
Expand Down Expand Up @@ -355,11 +357,12 @@ protected final MergedContextConfiguration createModifiedConfig(MergedContextCon
*/
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());
}

}
Expand Up @@ -23,8 +23,11 @@
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;
Expand All @@ -50,15 +53,33 @@ void springBootTestWithAMockWebEnvironmentCanBeUsedWithWebAppConfiguration() {
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)
Expand All @@ -73,4 +94,19 @@ static class SpringBootTestMockWebEnvironmentAndWebAppConfiguration {

}

@SpringBootTest(args = "--app.test=same")
static class SpringBootTestArgsConfiguration {

}

@SpringBootTest(args = "--app.test=same")
static class SpringBootTestSameArgsConfiguration {

}

@SpringBootTest(args = "--app.test=different")
static class SpringBootTestOtherArgsConfiguration {

}

}

0 comments on commit 4dc9bbe

Please sign in to comment.