Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot inject ServletContext into MockMultipartHttpServletRequest [SPR-10098] #14731

spring-issuemaster opened this issue Dec 15, 2012 · 1 comment


Copy link

@spring-issuemaster spring-issuemaster commented Dec 15, 2012

Dimitri De Franciscis opened SPR-10098 and commented

Status Quo

The servletContext field in MockHttpServletRequest is private but can be injected via public constructors. In other words, there's no setServletContext() method. This is useful, for example, if you want to inject a WebApplicationContext and/or a DispatcherServlet.

MockMultipartHttpServletRequest, unfortunately, hides this possibility because it has only an empty constructor.

Additional Issues with Mocks in Spring MVC Test

This problem is reflected also in Servlet 3.0 classes in the Spring MVC Test framework -- for example, Servlet3MockMultipartHttpServletRequest.

Both MockHttpServletRequestBuilder and MockMultipartHttpServletRequestBuilder have a createServlet3Request() method, but only MockHttpServletRequestBuilder passes the servletContext parameter to the constructor.

Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext);
Constructor<?> constructor = clazz.getDeclaredConstructor();
return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);


  • (/) Introduce a MockMultipartHttpServletRequest constructor that calls MockHttpServletRequest(ServletContext servletContext).
    *- This is what has been implemented.
  • (x) Introduce a setServletContext(ServletContext) method in MockHttpServletRequest.
    *- The servletContext field is final and should remain immutable. Thus it is not possible to introduce a setter method.

Affects: 3.2 GA

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jun 9, 2013

Sam Brannen commented

Resolving this issue as Complete, since the requested improvements have already been resolved against the 4.0 branch with the upgrade of all Servlet API mocks to Servlet 3.0.

See GitHub commit deba32cad9 for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.