Skip to content

Commit bf9d23e

Browse files
committed
Consider SpringBootTest's web environment in context cache key
Previously, the web environment configured on `@SpringBootTest` was not part of the context cache key. As a result, two test classes that has identical configuration other than one using a MOCK web environment and the other using a DEFINED_PORT web environment would share a context when they should not do so. Classes that use MOCK and RANDOM_PORT were not affected as the use of RANDOM_PORT results in a property for the port being added to the environment. This commit adds a new ContextCustomizer, SpringBootTestWebEnvironment, that is used to capture the `webEnvironment` from `@SpringBootTest` and use it in its hashCode and equals implementations. This fixes the problem as all context customizers are evaluated when determing the equality of two context cache keys. Fixes gh-23085
1 parent d208ec9 commit bf9d23e

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ protected final MergedContextConfiguration createModifiedConfig(MergedContextCon
359359
Class<?>[] classes, String[] propertySourceProperties) {
360360
Set<ContextCustomizer> contextCustomizers = new LinkedHashSet<>(mergedConfig.getContextCustomizers());
361361
contextCustomizers.add(new SpringBootTestArgs(mergedConfig.getTestClass()));
362+
contextCustomizers.add(new SpringBootTestWebEnvironment(mergedConfig.getTestClass()));
362363
return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes,
363364
mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(),
364365
mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context;
18+
19+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
20+
import org.springframework.context.ConfigurableApplicationContext;
21+
import org.springframework.core.annotation.MergedAnnotations;
22+
import org.springframework.test.context.ContextCustomizer;
23+
import org.springframework.test.context.MergedContextConfiguration;
24+
25+
/**
26+
* {@link ContextCustomizer} to track the web environment that is used in a
27+
* {@link SpringBootTest}. The web environment is taken into account when evaluating a
28+
* {@link MergedContextConfiguration} to determine if a context can be shared between
29+
* tests.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
class SpringBootTestWebEnvironment implements ContextCustomizer {
34+
35+
private final WebEnvironment webEnvironment;
36+
37+
SpringBootTestWebEnvironment(Class<?> testClass) {
38+
this.webEnvironment = MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
39+
.get(SpringBootTest.class).getValue("webEnvironment", WebEnvironment.class).orElse(null);
40+
}
41+
42+
@Override
43+
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
44+
}
45+
46+
@Override
47+
public boolean equals(Object obj) {
48+
return (obj != null) && (getClass() == obj.getClass())
49+
&& this.webEnvironment == ((SpringBootTestWebEnvironment) obj).webEnvironment;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return (this.webEnvironment != null) ? this.webEnvironment.hashCode() : 0;
55+
}
56+
57+
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/bootstrap/SpringBootTestContextBootstrapperTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
7171
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
7272
}
7373

74+
@Test
75+
void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual() {
76+
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
77+
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
78+
TestContext otherContext = buildTestContext(SpringBootTestDefinedPortWebEnvironmentConfiguration.class);
79+
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
80+
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
81+
}
82+
83+
@Test
84+
void mergedContextConfigurationWhenWebEnvironmentsSameShouldtBeConsideredEqual() {
85+
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
86+
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
87+
TestContext otherContext = buildTestContext(SpringBootTestAnotherMockWebEnvironmentConfiguration.class);
88+
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
89+
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
90+
}
91+
7492
@SuppressWarnings("rawtypes")
7593
private TestContext buildTestContext(Class<?> testClass) {
7694
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
@@ -99,6 +117,21 @@ static class SpringBootTestArgsConfiguration {
99117

100118
}
101119

120+
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
121+
static class SpringBootTestMockWebEnvironmentConfiguration {
122+
123+
}
124+
125+
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
126+
static class SpringBootTestAnotherMockWebEnvironmentConfiguration {
127+
128+
}
129+
130+
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
131+
static class SpringBootTestDefinedPortWebEnvironmentConfiguration {
132+
133+
}
134+
102135
@SpringBootTest(args = "--app.test=same")
103136
static class SpringBootTestSameArgsConfiguration {
104137

0 commit comments

Comments
 (0)