Merge branch '2.3.x' into 2.4.x

Closes gh-24536
pull/24597/head
Phillip Webb 4 years ago
commit d6890e39a7

@ -135,7 +135,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
} }
private void addConfigAttributesClasses(ContextConfigurationAttributes configAttributes, Class<?>[] classes) { private void addConfigAttributesClasses(ContextConfigurationAttributes configAttributes, Class<?>[] classes) {
List<Class<?>> combined = new ArrayList<>(Arrays.asList(classes)); Set<Class<?>> combined = new LinkedHashSet<>(Arrays.asList(classes));
if (configAttributes.getClasses() != null) { if (configAttributes.getClasses() != null) {
combined.addAll(Arrays.asList(configAttributes.getClasses())); combined.addAll(Arrays.asList(configAttributes.getClasses()));
} }

@ -23,6 +23,7 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.test.context.BootstrapContext; import org.springframework.test.context.BootstrapContext;
import org.springframework.test.context.CacheAwareContextLoaderDelegate; import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
@ -56,39 +57,47 @@ class SpringBootTestContextBootstrapperTests {
@Test @Test
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() { void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class); TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration"); MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class); TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration"); MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration); assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
} }
@Test @Test
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() { void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class); TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration"); MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class); TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration"); MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration); assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
} }
@Test @Test
void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual() { void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class); TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration"); MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext = buildTestContext(SpringBootTestDefinedPortWebEnvironmentConfiguration.class); TestContext otherContext = buildTestContext(SpringBootTestDefinedPortWebEnvironmentConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration"); MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration); assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
} }
@Test @Test
void mergedContextConfigurationWhenWebEnvironmentsSameShouldtBeConsideredEqual() { void mergedContextConfigurationWhenWebEnvironmentsSameShouldBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class); TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration"); MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext = buildTestContext(SpringBootTestAnotherMockWebEnvironmentConfiguration.class); TestContext otherContext = buildTestContext(SpringBootTestAnotherMockWebEnvironmentConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration"); MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration); assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
} }
@Test
void mergedContextConfigurationClassesShouldNotContainDuplicates() {
TestContext context = buildTestContext(SpringBootTestClassesConfiguration.class);
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
Class<?>[] classes = contextConfiguration.getClasses();
assertThat(classes).containsExactly(SpringBootTestContextBootstrapperExampleConfig.class);
}
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private TestContext buildTestContext(Class<?> testClass) { private TestContext buildTestContext(Class<?> testClass) {
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper(); SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
@ -100,6 +109,10 @@ class SpringBootTestContextBootstrapperTests {
return bootstrapper.buildTestContext(); return bootstrapper.buildTestContext();
} }
private MergedContextConfiguration getMergedContextConfiguration(TestContext context) {
return (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration");
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@WebAppConfiguration @WebAppConfiguration
static class SpringBootTestNonMockWebEnvironmentAndWebAppConfiguration { static class SpringBootTestNonMockWebEnvironmentAndWebAppConfiguration {
@ -142,4 +155,9 @@ class SpringBootTestContextBootstrapperTests {
} }
@SpringBootTest(classes = SpringBootTestContextBootstrapperExampleConfig.class)
static class SpringBootTestClassesConfiguration {
}
} }

Loading…
Cancel
Save