2323import org .springframework .boot .test .context .SpringBootTestContextBootstrapper ;
2424import org .springframework .test .context .BootstrapContext ;
2525import org .springframework .test .context .CacheAwareContextLoaderDelegate ;
26+ import org .springframework .test .context .MergedContextConfiguration ;
2627import org .springframework .test .context .TestContext ;
2728import org .springframework .test .context .web .WebAppConfiguration ;
2829import 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