public class MockServletContext extends Object implements ServletContext
ServletContext interface.
As of Spring 5.0, this set of mocks is designed on a Servlet 4.0 baseline.
Compatible with Servlet 3.1 but can be configured to expose a specific version
through setMajorVersion(int)/setMinorVersion(int); default is 3.1.
Note that Servlet 3.1 support is limited: servlet, filter and listener
registration methods are not supported; neither is JSP configuration.
We generally do not recommend to unit test your ServletContainerInitializers and
WebApplicationInitializers which is where those registration methods would be used.
For setting up a full WebApplicationContext in a test environment, you can
use AnnotationConfigWebApplicationContext, XmlWebApplicationContext,
or GenericWebApplicationContext, passing in a corresponding
MockServletContext instance. Consider configuring your
MockServletContext with a FileSystemResourceLoader in order to
interpret resource paths as relative filesystem locations.
MockServletContext(org.springframework.core.io.ResourceLoader),
AnnotationConfigWebApplicationContext,
XmlWebApplicationContext,
GenericWebApplicationContextORDERED_LIBS, TEMPDIR| Constructor and Description |
|---|
MockServletContext()
Create a new
MockServletContext, using no base path and a
DefaultResourceLoader (i.e. |
MockServletContext(ResourceLoader resourceLoader)
Create a new
MockServletContext, using the specified ResourceLoader
and no base path. |
MockServletContext(String resourceBasePath)
Create a new
MockServletContext, using a DefaultResourceLoader. |
MockServletContext(String resourceBasePath,
ResourceLoader resourceLoader)
Create a new
MockServletContext using the supplied resource base
path and resource loader. |
public MockServletContext()
MockServletContext, using no base path and a
DefaultResourceLoader (i.e. the classpath root as WAR root).DefaultResourceLoaderpublic MockServletContext(String resourceBasePath)
MockServletContext, using a DefaultResourceLoader.resourceBasePath - the root directory of the WAR (should not end with a slash)DefaultResourceLoaderpublic MockServletContext(@Nullable ResourceLoader resourceLoader)
MockServletContext, using the specified ResourceLoader
and no base path.resourceLoader - the ResourceLoader to use (or null for the default)public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader)
MockServletContext using the supplied resource base
path and resource loader.
Registers a MockRequestDispatcher for the Servlet named
'default'.
resourceBasePath - the root directory of the WAR (should not end with a slash)resourceLoader - the ResourceLoader to use (or null for the default)registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)protected String getResourceLocation(String path)
MockServletContext.path - the path as specifiedpublic void setContextPath(String contextPath)
public String getContextPath()
getContextPath in interface ServletContextpublic void registerContext(String contextPath, ServletContext context)
public ServletContext getContext(String contextPath)
getContext in interface ServletContextpublic void setMajorVersion(int majorVersion)
public int getMajorVersion()
getMajorVersion in interface ServletContextpublic void setMinorVersion(int minorVersion)
public int getMinorVersion()
getMinorVersion in interface ServletContextpublic void setEffectiveMajorVersion(int effectiveMajorVersion)
public int getEffectiveMajorVersion()
getEffectiveMajorVersion in interface ServletContextpublic void setEffectiveMinorVersion(int effectiveMinorVersion)
public int getEffectiveMinorVersion()
getEffectiveMinorVersion in interface ServletContext@Nullable public String getMimeType(String filePath)
getMimeType in interface ServletContextpublic void addMimeType(String fileExtension, MediaType mimeType)
getMimeType(String).fileExtension - a file extension, such as txt, gifmimeType - the mime type@Nullable public Set<String> getResourcePaths(String path)
getResourcePaths in interface ServletContext@Nullable public URL getResource(String path) throws MalformedURLException
getResource in interface ServletContextMalformedURLException@Nullable public InputStream getResourceAsStream(String path)
getResourceAsStream in interface ServletContextpublic RequestDispatcher getRequestDispatcher(String path)
getRequestDispatcher in interface ServletContextpublic RequestDispatcher getNamedDispatcher(String path)
getNamedDispatcher in interface ServletContextpublic void registerNamedDispatcher(String name, RequestDispatcher requestDispatcher)
RequestDispatcher (typically a MockRequestDispatcher)
that acts as a wrapper for the named Servlet.name - the name of the wrapped ServletrequestDispatcher - the dispatcher that wraps the named ServletgetNamedDispatcher(java.lang.String),
unregisterNamedDispatcher(java.lang.String)public void unregisterNamedDispatcher(String name)
RequestDispatcher with the given name.name - the name of the dispatcher to unregistergetNamedDispatcher(java.lang.String),
registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)public String getDefaultServletName()
Servlet.
Defaults to 'default'.
setDefaultServletName(java.lang.String)public void setDefaultServletName(String defaultServletName)
Servlet.
Also unregisters the current default
RequestDispatcher and replaces
it with a MockRequestDispatcher for the provided
defaultServletName.
defaultServletName - the name of the default Servlet;
never null or emptygetDefaultServletName()@Deprecated @Nullable public Servlet getServlet(String name)
getServlet in interface ServletContext@Deprecated public Enumeration<Servlet> getServlets()
getServlets in interface ServletContext@Deprecated public Enumeration<String> getServletNames()
getServletNames in interface ServletContextpublic void log(String message)
log in interface ServletContext@Deprecated public void log(Exception ex, String message)
log in interface ServletContextpublic void log(String message, Throwable ex)
log in interface ServletContext@Nullable public String getRealPath(String path)
getRealPath in interface ServletContextpublic String getServerInfo()
getServerInfo in interface ServletContextpublic String getInitParameter(String name)
getInitParameter in interface ServletContextpublic Enumeration<String> getInitParameterNames()
getInitParameterNames in interface ServletContextpublic boolean setInitParameter(String name, String value)
setInitParameter in interface ServletContext@Nullable public Object getAttribute(String name)
getAttribute in interface ServletContextpublic Enumeration<String> getAttributeNames()
getAttributeNames in interface ServletContextpublic void setAttribute(String name, @Nullable Object value)
setAttribute in interface ServletContextpublic void removeAttribute(String name)
removeAttribute in interface ServletContextpublic void setServletContextName(String servletContextName)
public String getServletContextName()
getServletContextName in interface ServletContext@Nullable public ClassLoader getClassLoader()
getClassLoader in interface ServletContextpublic void declareRoles(String... roleNames)
declareRoles in interface ServletContextpublic void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
setSessionTrackingModes in interface ServletContextIllegalStateExceptionIllegalArgumentExceptionpublic Set<SessionTrackingMode> getDefaultSessionTrackingModes()
getDefaultSessionTrackingModes in interface ServletContextpublic Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
getEffectiveSessionTrackingModes in interface ServletContextpublic SessionCookieConfig getSessionCookieConfig()
getSessionCookieConfig in interface ServletContextpublic void setSessionTimeout(int sessionTimeout)
setSessionTimeout in interface ServletContextpublic int getSessionTimeout()
getSessionTimeout in interface ServletContextpublic void setRequestCharacterEncoding(@Nullable String requestCharacterEncoding)
setRequestCharacterEncoding in interface ServletContext@Nullable public String getRequestCharacterEncoding()
getRequestCharacterEncoding in interface ServletContextpublic void setResponseCharacterEncoding(@Nullable String responseCharacterEncoding)
setResponseCharacterEncoding in interface ServletContext@Nullable public String getResponseCharacterEncoding()
getResponseCharacterEncoding in interface ServletContextpublic JspConfigDescriptor getJspConfigDescriptor()
getJspConfigDescriptor in interface ServletContextpublic ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
addJspFile in interface ServletContextpublic ServletRegistration.Dynamic addServlet(String servletName, String className)
addServlet in interface ServletContextpublic ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet)
addServlet in interface ServletContextpublic ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass)
addServlet in interface ServletContextpublic <T extends Servlet> T createServlet(Class<T> c) throws ServletException
createServlet in interface ServletContextServletException@Nullable public ServletRegistration getServletRegistration(String servletName)
null.getServletRegistration in interface ServletContextServletContext.getServletRegistration(java.lang.String)public Map<String,? extends ServletRegistration> getServletRegistrations()
getServletRegistrations in interface ServletContextServletContext.getServletRegistrations()public FilterRegistration.Dynamic addFilter(String filterName, String className)
addFilter in interface ServletContextpublic FilterRegistration.Dynamic addFilter(String filterName, Filter filter)
addFilter in interface ServletContextpublic FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass)
addFilter in interface ServletContextpublic <T extends Filter> T createFilter(Class<T> c) throws ServletException
createFilter in interface ServletContextServletException@Nullable public FilterRegistration getFilterRegistration(String filterName)
null.getFilterRegistration in interface ServletContextServletContext.getFilterRegistration(java.lang.String)public Map<String,? extends FilterRegistration> getFilterRegistrations()
getFilterRegistrations in interface ServletContextServletContext.getFilterRegistrations()public void addListener(Class<? extends EventListener> listenerClass)
addListener in interface ServletContextpublic void addListener(String className)
addListener in interface ServletContextpublic <T extends EventListener> void addListener(T t)
addListener in interface ServletContextpublic <T extends EventListener> T createListener(Class<T> c) throws ServletException
createListener in interface ServletContextServletExceptionpublic String getVirtualServerName()
getVirtualServerName in interface ServletContext