23
23
import org .springframework .boot .test .context .SpringBootTestContextBootstrapper ;
24
24
import org .springframework .test .context .BootstrapContext ;
25
25
import org .springframework .test .context .CacheAwareContextLoaderDelegate ;
26
+ import org .springframework .test .context .MergedContextConfiguration ;
26
27
import org .springframework .test .context .TestContext ;
27
28
import org .springframework .test .context .web .WebAppConfiguration ;
28
29
import org .springframework .test .util .ReflectionTestUtils ;
@@ -56,39 +57,47 @@ void springBootTestWithAMockWebEnvironmentCanBeUsedWithWebAppConfiguration() {
56
57
@ Test
57
58
void mergedContextConfigurationWhenArgsDifferentShouldNotBeConsideredEqual () {
58
59
TestContext context = buildTestContext (SpringBootTestArgsConfiguration .class );
59
- Object contextConfiguration = ReflectionTestUtils . getField (context , "mergedContextConfiguration" );
60
+ MergedContextConfiguration contextConfiguration = getMergedContextConfiguration (context );
60
61
TestContext otherContext2 = buildTestContext (SpringBootTestOtherArgsConfiguration .class );
61
- Object otherContextConfiguration = ReflectionTestUtils . getField (otherContext2 , "mergedContextConfiguration" );
62
+ MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration (otherContext2 );
62
63
assertThat (contextConfiguration ).isNotEqualTo (otherContextConfiguration );
63
64
}
64
65
65
66
@ Test
66
67
void mergedContextConfigurationWhenArgsSameShouldBeConsideredEqual () {
67
68
TestContext context = buildTestContext (SpringBootTestArgsConfiguration .class );
68
- Object contextConfiguration = ReflectionTestUtils . getField (context , "mergedContextConfiguration" );
69
+ MergedContextConfiguration contextConfiguration = getMergedContextConfiguration (context );
69
70
TestContext otherContext2 = buildTestContext (SpringBootTestSameArgsConfiguration .class );
70
- Object otherContextConfiguration = ReflectionTestUtils . getField (otherContext2 , "mergedContextConfiguration" );
71
+ MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration (otherContext2 );
71
72
assertThat (contextConfiguration ).isEqualTo (otherContextConfiguration );
72
73
}
73
74
74
75
@ Test
75
76
void mergedContextConfigurationWhenWebEnvironmentsDifferentShouldNotBeConsideredEqual () {
76
77
TestContext context = buildTestContext (SpringBootTestMockWebEnvironmentConfiguration .class );
77
- Object contextConfiguration = ReflectionTestUtils . getField (context , "mergedContextConfiguration" );
78
+ MergedContextConfiguration contextConfiguration = getMergedContextConfiguration (context );
78
79
TestContext otherContext = buildTestContext (SpringBootTestDefinedPortWebEnvironmentConfiguration .class );
79
- Object otherContextConfiguration = ReflectionTestUtils . getField (otherContext , "mergedContextConfiguration" );
80
+ MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration (otherContext );
80
81
assertThat (contextConfiguration ).isNotEqualTo (otherContextConfiguration );
81
82
}
82
83
83
84
@ Test
84
- void mergedContextConfigurationWhenWebEnvironmentsSameShouldtBeConsideredEqual () {
85
+ void mergedContextConfigurationWhenWebEnvironmentsSameShouldBeConsideredEqual () {
85
86
TestContext context = buildTestContext (SpringBootTestMockWebEnvironmentConfiguration .class );
86
- Object contextConfiguration = ReflectionTestUtils . getField (context , "mergedContextConfiguration" );
87
+ MergedContextConfiguration contextConfiguration = getMergedContextConfiguration (context );
87
88
TestContext otherContext = buildTestContext (SpringBootTestAnotherMockWebEnvironmentConfiguration .class );
88
- Object otherContextConfiguration = ReflectionTestUtils . getField (otherContext , "mergedContextConfiguration" );
89
+ MergedContextConfiguration otherContextConfiguration = getMergedContextConfiguration (otherContext );
89
90
assertThat (contextConfiguration ).isEqualTo (otherContextConfiguration );
90
91
}
91
92
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
+
92
101
@ SuppressWarnings ("rawtypes" )
93
102
private TestContext buildTestContext (Class <?> testClass ) {
94
103
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper ();
@@ -100,6 +109,10 @@ private TestContext buildTestContext(Class<?> testClass) {
100
109
return bootstrapper .buildTestContext ();
101
110
}
102
111
112
+ private MergedContextConfiguration getMergedContextConfiguration (TestContext context ) {
113
+ return (MergedContextConfiguration ) ReflectionTestUtils .getField (context , "mergedContextConfiguration" );
114
+ }
115
+
103
116
@ SpringBootTest (webEnvironment = WebEnvironment .RANDOM_PORT )
104
117
@ WebAppConfiguration
105
118
static class SpringBootTestNonMockWebEnvironmentAndWebAppConfiguration {
@@ -142,4 +155,9 @@ static class SpringBootTestOtherArgsConfiguration {
142
155
143
156
}
144
157
158
+ @ SpringBootTest (classes = SpringBootTestContextBootstrapperExampleConfig .class )
159
+ static class SpringBootTestClassesConfiguration {
160
+
161
+ }
162
+
145
163
}
0 commit comments