Skip to content

Commit c2b0fac

Browse files
committed
Add extension point to pre-configure a MockMvcBuilder
Issue: SPR-11497
1 parent d86e4cf commit c2b0fac

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,8 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
5050

5151
private Boolean dispatchOptions = Boolean.FALSE;
5252

53+
private final List<MockMvcConfigurer> configurers = new ArrayList<MockMvcConfigurer>(4);
54+
5355

5456

5557
/**
@@ -166,16 +168,28 @@ public final <T extends B> T alwaysDo(ResultHandler resultHandler) {
166168
}
167169

168170
/**
169-
* Should the {@link org.springframework.web.servlet.DispatcherServlet} dispatch OPTIONS request to controllers.
170-
* @param dispatchOptions
171-
* @see org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest(boolean)
171+
* Whether to enable the DispatcherServlet property
172+
* {@link org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest
173+
* dispatchOptionsRequest} which allows processing of HTTP OPTIONS requests.
172174
*/
173175
@SuppressWarnings("unchecked")
174176
public final <T extends B> T dispatchOptions(boolean dispatchOptions) {
175177
this.dispatchOptions = dispatchOptions;
176178
return (T) this;
177179
}
178180

181+
/**
182+
* Add a {@code MockMvcConfigurer} which encapsulates ways to further configure
183+
* this MockMvcBuilder with some specific purpose in mind.
184+
*/
185+
@SuppressWarnings("unchecked")
186+
public final <T extends B> T add(MockMvcConfigurer configurer) {
187+
configurer.afterConfigurerAdded(this);
188+
this.configurers.add(configurer);
189+
return (T) this;
190+
}
191+
192+
179193
/**
180194
* Build a {@link org.springframework.test.web.servlet.MockMvc} instance.
181195
*/
@@ -187,6 +201,10 @@ public final MockMvc build() {
187201
ServletContext servletContext = wac.getServletContext();
188202
MockServletConfig mockServletConfig = new MockServletConfig(servletContext);
189203

204+
for (MockMvcConfigurer configurer : this.configurers) {
205+
configurer.beforeMockMvcCreated(this, this.defaultRequestBuilder, wac);
206+
}
207+
190208
Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]);
191209

192210
return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.test.web.servlet.setup;
17+
18+
import org.springframework.test.web.servlet.MockMvcBuilder;
19+
import org.springframework.test.web.servlet.RequestBuilder;
20+
import org.springframework.web.context.WebApplicationContext;
21+
22+
/**
23+
* A contract that allows the encapsulation of a "recipe" for configuring a
24+
* MockMvcBuilder for some specific purpose. For example a 3rd party library
25+
* may use this to provide convenient, easy ways to set up MockMvc.
26+
*
27+
* <p>Supported via {@link AbstractMockMvcBuilder#add(MockMvcConfigurer)}
28+
* with instances of class likely created via static methods, e.g.:
29+
*
30+
* <pre class="code">
31+
* MockMvcBuilders.webAppContextSetup(context)
32+
* .add(myLibrary("foo","bar").myProperty("foo"))
33+
* .build();
34+
* </pre>
35+
*
36+
* @author Rossen Stoyanchev
37+
* @since 4.1
38+
*/
39+
public interface MockMvcConfigurer {
40+
41+
42+
/**
43+
* Invoked immediately after a {@code MockMvcConfigurer} is configured via
44+
* {@link AbstractMockMvcBuilder#add(MockMvcConfigurer)}.
45+
*/
46+
void afterConfigurerAdded(MockMvcBuilder mockMvcBuilder);
47+
48+
/**
49+
* Invoked just before the MockMvc instance is built providing access to the
50+
* configured "default" RequestBuilder. If a "default" RequestBuilder is
51+
* needed but was not configured and is {@code null}), it can still be added
52+
* via {@link AbstractMockMvcBuilder#defaultRequest}.
53+
*/
54+
void beforeMockMvcCreated(MockMvcBuilder mockMvcBuilder, RequestBuilder defaultRequestBuilder,
55+
WebApplicationContext applicationContext);
56+
57+
}

0 commit comments

Comments
 (0)