Skip to content

Commit e626f7f

Browse files
committed
Merge branch '2.2.x' into 2.3.x
Closes gh-23477
2 parents 184bd5b + bf9d23e commit e626f7f

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)