35
35
import org .springframework .web .servlet .DispatcherServlet ;
36
36
37
37
import static org .assertj .core .api .Assertions .assertThat ;
38
+ import static org .mockito .Mockito .mock ;
38
39
39
40
/**
40
41
* Tests for {@link DispatcherServletAutoConfiguration}.
@@ -65,6 +66,8 @@ public void registrationNonServletBean() {
65
66
.run ((context ) -> {
66
67
assertThat (context ).doesNotHaveBean (ServletRegistrationBean .class );
67
68
assertThat (context ).doesNotHaveBean (DispatcherServlet .class );
69
+ assertThat (context )
70
+ .doesNotHaveBean (DispatcherServletPathProvider .class );
68
71
});
69
72
}
70
73
@@ -73,7 +76,8 @@ public void registrationNonServletBean() {
73
76
@ Test
74
77
public void registrationOverrideWithDispatcherServletWrongName () {
75
78
this .contextRunner
76
- .withUserConfiguration (CustomDispatcherServletDifferentName .class )
79
+ .withUserConfiguration (CustomDispatcherServletDifferentName .class ,
80
+ CustomDispatcherServletPathProvider .class )
77
81
.run ((context ) -> {
78
82
ServletRegistrationBean <?> registration = context
79
83
.getBean (ServletRegistrationBean .class );
@@ -86,8 +90,8 @@ public void registrationOverrideWithDispatcherServletWrongName() {
86
90
87
91
@ Test
88
92
public void registrationOverrideWithAutowiredServlet () {
89
- this .contextRunner .withUserConfiguration (CustomAutowiredRegistration .class )
90
- .run ((context ) -> {
93
+ this .contextRunner .withUserConfiguration (CustomAutowiredRegistration .class ,
94
+ CustomDispatcherServletPathProvider . class ) .run ((context ) -> {
91
95
ServletRegistrationBean <?> registration = context
92
96
.getBean (ServletRegistrationBean .class );
93
97
assertThat (registration .getUrlMappings ()).containsExactly ("/foo" );
@@ -112,6 +116,40 @@ public void servletPath() {
112
116
});
113
117
}
114
118
119
+ @ Test
120
+ public void pathProviderNotCreatedWhenMultipleDispatcherServletsPresent () {
121
+ this .contextRunner
122
+ .withUserConfiguration (CustomDispatcherServletDifferentName .class )
123
+ .run ((context ) -> assertThat (context )
124
+ .doesNotHaveBean (DispatcherServletPathProvider .class ));
125
+ }
126
+
127
+ @ Test
128
+ public void pathProviderWhenCustomDispatcherServletSameNameShouldReturnConfiguredServletPath () {
129
+ this .contextRunner .withUserConfiguration (CustomDispatcherServletSameName .class )
130
+ .withPropertyValues ("server.servlet.path:/spring" )
131
+ .run ((context ) -> assertThat (context
132
+ .getBean (DispatcherServletPathProvider .class ).getServletPath ())
133
+ .isEqualTo ("/spring" ));
134
+ }
135
+
136
+ @ Test
137
+ public void pathProviderNotCreatedWhenDefaultDispatcherServletNotAvailable () {
138
+ this .contextRunner
139
+ .withUserConfiguration (CustomDispatcherServletDifferentName .class ,
140
+ NonServletConfiguration .class )
141
+ .run ((context ) -> assertThat (context )
142
+ .doesNotHaveBean (DispatcherServletPathProvider .class ));
143
+ }
144
+
145
+ @ Test
146
+ public void pathProviderNotCreatedWhenCustomRegistrationBeanPresent () {
147
+ this .contextRunner
148
+ .withUserConfiguration (CustomDispatcherServletRegistration .class )
149
+ .run ((context ) -> assertThat (context )
150
+ .doesNotHaveBean (DispatcherServletPathProvider .class ));
151
+ }
152
+
115
153
@ Test
116
154
public void multipartConfig () {
117
155
this .contextRunner .withUserConfiguration (MultipartConfiguration .class )
@@ -198,6 +236,16 @@ public DispatcherServlet customDispatcherServlet() {
198
236
199
237
}
200
238
239
+ @ Configuration
240
+ protected static class CustomDispatcherServletPathProvider {
241
+
242
+ @ Bean
243
+ public DispatcherServletPathProvider dispatcherServletPathProvider () {
244
+ return mock (DispatcherServletPathProvider .class );
245
+ }
246
+
247
+ }
248
+
201
249
@ Configuration
202
250
protected static class CustomAutowiredRegistration {
203
251
@@ -210,6 +258,11 @@ public ServletRegistrationBean<?> dispatcherServletRegistration(
210
258
return registration ;
211
259
}
212
260
261
+ @ Bean
262
+ public DispatcherServletPathProvider dispatcherServletPathProvider () {
263
+ return mock (DispatcherServletPathProvider .class );
264
+ }
265
+
213
266
}
214
267
215
268
@ Configuration
@@ -232,6 +285,30 @@ public MultipartResolver getMultipartResolver() {
232
285
233
286
}
234
287
288
+ @ Configuration
289
+ protected static class CustomDispatcherServletSameName {
290
+
291
+ @ Bean (name = DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_BEAN_NAME )
292
+ public DispatcherServlet dispatcherServlet () {
293
+ return new DispatcherServlet ();
294
+ }
295
+
296
+ }
297
+
298
+ @ Configuration
299
+ protected static class CustomDispatcherServletRegistration {
300
+
301
+ @ Bean (name = DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME )
302
+ public ServletRegistrationBean <DispatcherServlet > dispatcherServletRegistration (
303
+ DispatcherServlet dispatcherServlet ) {
304
+ ServletRegistrationBean <DispatcherServlet > registration = new ServletRegistrationBean <>(
305
+ dispatcherServlet , "/foo" );
306
+ registration .setName ("customDispatcher" );
307
+ return registration ;
308
+ }
309
+
310
+ }
311
+
235
312
private static class MockMultipartResolver implements MultipartResolver {
236
313
237
314
@ Override
0 commit comments