Skip to content

Commit b3ccefd

Browse files
committed
Merge branch '2.2.x'
Closes gh-21102
2 parents ddcd1bc + 4dc9bbe commit b3ccefd

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-9
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878
*/
7979
public class SpringBootContextLoader extends AbstractContextLoader {
8080

81-
private static final String[] NO_ARGS = new String[0];
82-
8381
@Override
8482
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
8583
Class<?>[] configClasses = config.getClasses();
@@ -145,11 +143,16 @@ protected ConfigurableEnvironment getEnvironment() {
145143
* empty array.
146144
* @param config the source context configuration
147145
* @return the application arguments to use
146+
* @deprecated since 2.2.7
148147
* @see SpringApplication#run(String...)
149148
*/
150149
protected String[] getArgs(MergedContextConfiguration config) {
151-
return MergedAnnotations.from(config.getTestClass(), SearchStrategy.TYPE_HIERARCHY).get(SpringBootTest.class)
152-
.getValue("args", String[].class).orElse(NO_ARGS);
150+
ContextCustomizer customizer = config.getContextCustomizers().stream()
151+
.filter((c) -> c instanceof SpringBootTestArgsTrackingContextCustomizer).findFirst().orElse(null);
152+
if (customizer != null) {
153+
return ((SpringBootTestArgsTrackingContextCustomizer) customizer).getArgs();
154+
}
155+
return SpringBootTestArgsTrackingContextCustomizer.NO_ARGS;
153156
}
154157

155158
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 java.util.Arrays;
20+
21+
import org.springframework.context.ConfigurableApplicationContext;
22+
import org.springframework.core.annotation.MergedAnnotations;
23+
import org.springframework.test.context.ContextCustomizer;
24+
import org.springframework.test.context.MergedContextConfiguration;
25+
26+
/**
27+
* {@link ContextCustomizer} to track application arguments that are used in a
28+
* {@link SpringBootTest}. The application arguments are taken into account when
29+
* evaluating a {@link MergedContextConfiguration} to determine if a context can be shared
30+
* between tests.
31+
*
32+
* @author Madhura Bhave
33+
*/
34+
class SpringBootTestArgsTrackingContextCustomizer implements ContextCustomizer {
35+
36+
static final String[] NO_ARGS = new String[0];
37+
38+
private String[] args;
39+
40+
SpringBootTestArgsTrackingContextCustomizer(Class<?> testClass) {
41+
this.args = MergedAnnotations.from(testClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
42+
.get(SpringBootTest.class).getValue("args", String[].class).orElse(NO_ARGS);
43+
}
44+
45+
@Override
46+
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
47+
48+
}
49+
50+
/**
51+
* Return the application arguments that are tracked by this customizer.
52+
* @return the args
53+
*/
54+
String[] getArgs() {
55+
return this.args;
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
return (obj != null) && (getClass() == obj.getClass())
61+
&& Arrays.equals(this.args, ((SpringBootTestArgsTrackingContextCustomizer) obj).args);
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Arrays.hashCode(this.args);
67+
}
68+
69+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.HashSet;
22+
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Set;
2425

@@ -40,6 +41,7 @@
4041
import org.springframework.core.io.support.SpringFactoriesLoader;
4142
import org.springframework.test.context.ContextConfiguration;
4243
import org.springframework.test.context.ContextConfigurationAttributes;
44+
import org.springframework.test.context.ContextCustomizer;
4345
import org.springframework.test.context.ContextHierarchy;
4446
import org.springframework.test.context.ContextLoader;
4547
import org.springframework.test.context.MergedContextConfiguration;
@@ -355,11 +357,12 @@ protected final MergedContextConfiguration createModifiedConfig(MergedContextCon
355357
*/
356358
protected final MergedContextConfiguration createModifiedConfig(MergedContextConfiguration mergedConfig,
357359
Class<?>[] classes, String[] propertySourceProperties) {
360+
Set<ContextCustomizer> contextCustomizers = new LinkedHashSet<>(mergedConfig.getContextCustomizers());
361+
contextCustomizers.add(new SpringBootTestArgsTrackingContextCustomizer(mergedConfig.getTestClass()));
358362
return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes,
359363
mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(),
360-
mergedConfig.getPropertySourceLocations(), propertySourceProperties,
361-
mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(),
362-
getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
364+
mergedConfig.getPropertySourceLocations(), propertySourceProperties, contextCustomizers,
365+
mergedConfig.getContextLoader(), getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
363366
}
364367

365368
}

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
2424
import org.springframework.test.context.BootstrapContext;
2525
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
26+
import org.springframework.test.context.TestContext;
2627
import org.springframework.test.context.web.WebAppConfiguration;
28+
import org.springframework.test.util.ReflectionTestUtils;
2729

30+
import static org.assertj.core.api.Assertions.assertThat;
2831
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2932
import static org.mockito.BDDMockito.given;
3033
import static org.mockito.Mockito.mock;
@@ -50,15 +53,33 @@ void springBootTestWithAMockWebEnvironmentCanBeUsedWithWebAppConfiguration() {
5053
buildTestContext(SpringBootTestMockWebEnvironmentAndWebAppConfiguration.class);
5154
}
5255

56+
@Test
57+
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() {
58+
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
59+
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
60+
TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class);
61+
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
62+
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
63+
}
64+
65+
@Test
66+
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
67+
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
68+
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
69+
TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class);
70+
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
71+
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
72+
}
73+
5374
@SuppressWarnings("rawtypes")
54-
private void buildTestContext(Class<?> testClass) {
75+
private TestContext buildTestContext(Class<?> testClass) {
5576
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
5677
BootstrapContext bootstrapContext = mock(BootstrapContext.class);
5778
bootstrapper.setBootstrapContext(bootstrapContext);
5879
given((Class) bootstrapContext.getTestClass()).willReturn(testClass);
5980
CacheAwareContextLoaderDelegate contextLoaderDelegate = mock(CacheAwareContextLoaderDelegate.class);
6081
given(bootstrapContext.getCacheAwareContextLoaderDelegate()).willReturn(contextLoaderDelegate);
61-
bootstrapper.buildTestContext();
82+
return bootstrapper.buildTestContext();
6283
}
6384

6485
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@@ -73,4 +94,19 @@ static class SpringBootTestMockWebEnvironmentAndWebAppConfiguration {
7394

7495
}
7596

97+
@SpringBootTest(args = "--app.test=same")
98+
static class SpringBootTestArgsConfiguration {
99+
100+
}
101+
102+
@SpringBootTest(args = "--app.test=same")
103+
static class SpringBootTestSameArgsConfiguration {
104+
105+
}
106+
107+
@SpringBootTest(args = "--app.test=different")
108+
static class SpringBootTestOtherArgsConfiguration {
109+
110+
}
111+
76112
}

0 commit comments

Comments
 (0)