Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicate classes in MergedContextConfiguration #24532

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -135,7 +135,7 @@ protected ContextLoader resolveContextLoader(Class<?> testClass,
}

private void addConfigAttributesClasses(ContextConfigurationAttributes configAttributes, Class<?>[] classes) {
List<Class<?>> combined = new ArrayList<>(Arrays.asList(classes));
Set<Class<?>> combined = new HashSet<>(Arrays.asList(classes));
dreis2211 marked this conversation as resolved.
Show resolved Hide resolved
if (configAttributes.getClasses() != null) {
combined.addAll(Arrays.asList(configAttributes.getClasses()));
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.test.context.BootstrapContext;
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.util.ReflectionTestUtils;
Expand Down Expand Up @@ -56,39 +57,47 @@ void springBootTestWithAMockWebEnvironmentCanBeUsedWithWebAppConfiguration() {
@Test
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext2 = buildTestContext(SpringBootTestOtherArgsConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
}

@Test
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestArgsConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext2 = buildTestContext(SpringBootTestSameArgsConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext2, "mergedContextConfiguration");
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext2);
assertThat(contextConfiguration).isEqualTo(otherContextConfiguration);
}

@Test
void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext = buildTestContext(SpringBootTestDefinedPortWebEnvironmentConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
assertThat(contextConfiguration).isNotEqualTo(otherContextConfiguration);
}

@Test
void mergedContextConfigurationWhenWebEnvironmentsSameShouldtBeConsideredEqual() {
void mergedContextConfigurationWhenWebEnvironmentsSameShouldBeConsideredEqual() {
TestContext context = buildTestContext(SpringBootTestMockWebEnvironmentConfiguration.class);
Object contextConfiguration = ReflectionTestUtils.getField(context, "mergedContextConfiguration");
MergedContextConfiguration contextConfiguration = getMergedContextConfiguration(context);
TestContext otherContext = buildTestContext(SpringBootTestAnotherMockWebEnvironmentConfiguration.class);
Object otherContextConfiguration = ReflectionTestUtils.getField(otherContext, "mergedContextConfiguration");
MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration(otherContext);
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")
private TestContext buildTestContext(Class<?> testClass) {
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
Expand All @@ -100,6 +109,10 @@ private TestContext buildTestContext(Class<?> testClass) {
return bootstrapper.buildTestContext();
}

private MergedContextConfiguration getMergedContextConfiguration(TestContext context) {
return (MergedContextConfiguration) ReflectionTestUtils.getField(context, "mergedContextConfiguration");
}

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@WebAppConfiguration
static class SpringBootTestNonMockWebEnvironmentAndWebAppConfiguration {
Expand Down Expand Up @@ -142,4 +155,9 @@ static class SpringBootTestOtherArgsConfiguration {

}

@SpringBootTest(classes = SpringBootTestContextBootstrapperExampleConfig.class)
static class SpringBootTestClassesConfiguration {

}

}