Skip to content

Commit 072e5e8

Browse files
committed
Revise use of FilterChain in MockMvc
MockFilterChain should not be re-used across requests. This is not an issue unless tests are executed concurrently. This change ensures the MockFilterChain is re-created for each request in MockMvc. Issue: SPR-10838
1 parent a17912d commit 072e5e8

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/MockMvc.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import javax.servlet.Filter;
2223
import javax.servlet.ServletContext;
2324

2425
import org.springframework.beans.Mergeable;
@@ -54,7 +55,9 @@ public final class MockMvc {
5455

5556
static String MVC_RESULT_ATTRIBUTE = MockMvc.class.getName().concat(".MVC_RESULT_ATTRIBUTE");
5657

57-
private final MockFilterChain filterChain;
58+
private final TestDispatcherServlet servlet;
59+
60+
private final Filter[] filters;
5861

5962
private final ServletContext servletContext;
6063

@@ -69,11 +72,15 @@ public final class MockMvc {
6972
* Private constructor, not for direct instantiation.
7073
* @see org.springframework.test.web.servlet.setup.MockMvcBuilders
7174
*/
72-
MockMvc(MockFilterChain filterChain, ServletContext servletContext) {
75+
MockMvc(TestDispatcherServlet servlet, Filter[] filters, ServletContext servletContext) {
76+
77+
Assert.notNull(servlet, "DispatcherServlet is required");
78+
Assert.notNull(filters, "filters cannot be null");
79+
Assert.noNullElements(filters, "filters cannot contain null values");
7380
Assert.notNull(servletContext, "A ServletContext is required");
74-
Assert.notNull(filterChain, "A MockFilterChain is required");
7581

76-
this.filterChain = filterChain;
82+
this.servlet = servlet;
83+
this.filters = filters;
7784
this.servletContext = servletContext;
7885
}
7986

@@ -130,8 +137,8 @@ public ResultActions perform(RequestBuilder requestBuilder) throws Exception {
130137
final MvcResult mvcResult = new DefaultMvcResult(request, response);
131138
request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);
132139

133-
this.filterChain.reset();
134-
this.filterChain.doFilter(request, response);
140+
MockFilterChain filterChain = new MockFilterChain(this.servlet, this.filters);
141+
filterChain.doFilter(request, response);
135142

136143
applyDefaultResultActions(mvcResult);
137144

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/MockMvcBuilderSupport.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.servlet.ServletException;
2424

2525
import org.springframework.core.NestedRuntimeException;
26-
import org.springframework.mock.web.MockFilterChain;
2726
import org.springframework.mock.web.MockServletConfig;
2827
import org.springframework.web.context.WebApplicationContext;
2928

@@ -57,9 +56,7 @@ protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servle
5756
throw new MockMvcBuildException("Failed to initialize TestDispatcherServlet", ex);
5857
}
5958

60-
MockFilterChain filterChain = new MockFilterChain(dispatcherServlet, filters);
61-
62-
MockMvc mockMvc = new MockMvc(filterChain, servletContext);
59+
MockMvc mockMvc = new MockMvc(dispatcherServlet, filters, servletContext);
6360
mockMvc.setDefaultRequest(defaultRequestBuilder);
6461
mockMvc.setGlobalResultMatchers(globalResultMatchers);
6562
mockMvc.setGlobalResultHandlers(globalResultHandlers);

0 commit comments

Comments
 (0)