Skip to content

Commit d6890e3

Browse files
committed
Merge branch '2.3.x' into 2.4.x
Closes gh-24536
2 parents dfcabe1 + 2cd1459 commit d6890e3

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected ContextLoader resolveContextLoader(Class<?> testClass,
135135
}
136136

137137
private void addConfigAttributesClasses(ContextConfigurationAttributes configAttributes, Class<?>[] classes) {
138-
List<Class<?>> combined = new ArrayList<>(Arrays.asList(classes));
138+
Set<Class<?>> combined = new LinkedHashSet<>(Arrays.asList(classes));
139139
if (configAttributes.getClasses() != null) {
140140
combined.addAll(Arrays.asList(configAttributes.getClasses()));
141141
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
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.MergedContextConfiguration;
2627
import org.springframework.test.context.TestContext;
2728
import org.springframework.test.context.web.WebAppConfiguration;
2829
import org.springframework.test.util.ReflectionTestUtils;
@@ -56,39 +57,47 @@ void springBootTestWithAMockWebEnvironmentCanBeUsedWithWebAppConfiguration() {
5657
@Test
5758
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() {
5859
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
59-
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
60+
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
6061
TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class);
61-
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
62+
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
6263
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
6364
}
6465

6566
@Test
6667
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
6768
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
68-
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
69+
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
6970
TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class);
70-
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
71+
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
7172
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
7273
}
7374

7475
@Test
7576
void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual() {
7677
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
77-
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
78+
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
7879
TestContext otherContext = buildTestContext(SpringBootTestDefinedPortWebEnvironmentConfiguration.class);
79-
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
80+
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
8081
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
8182
}
8283

8384
@Test
84-
void mergedContextConfigurationWhenWebEnvironmentsSameShouldtBeConsideredEqual() {
85+
void mergedContextConfigurationWhenWebEnvironmentsSameShouldBeConsideredEqual() {
8586
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
86-
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
87+
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
8788
TestContext otherContext = buildTestContext(SpringBootTestAnotherMockWebEnvironmentConfiguration.class);
88-
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
89+
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
8990
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
9091
}
9192

93+
@Test
94+
void mergedContextConfigurationClassesShouldNotContainDuplicates() {
95+
TestContext context = buildTestContext(SpringBootTestClassesConfiguration.class);
96+
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
97+
Class<?>[] classes = contextConfiguration.getClasses();
98+
assertThat(classes).containsExactly(SpringBootTestContextBootstrapperExampleConfig.class);
99+
}
100+
92101
@SuppressWarnings("rawtypes")
93102
private TestContext buildTestContext(Class<?> testClass) {
94103
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
@@ -100,6 +109,10 @@ private TestContext buildTestContext(Class<?> testClass) {
100109
return bootstrapper.buildTestContext();
101110
}
102111

112+
private MergedContextConfiguration getMergedContextConfiguration(TestContext context) {
113+
return (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration");
114+
}
115+
103116
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
104117
@WebAppConfiguration
105118
static class SpringBootTestNonMockWebEnvironmentAndWebAppConfiguration {
@@ -142,4 +155,9 @@ static class SpringBootTestOtherArgsConfiguration {
142155

143156
}
144157

158+
@SpringBootTest(classes = SpringBootTestContextBootstrapperExampleConfig.class)
159+
static class SpringBootTestClassesConfiguration {
160+
161+
}
162+
145163
}

0 commit comments

Comments
 (0)