public class MockServletContext extends java.lang.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
,
GenericWebApplicationContext
ORDERED_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(java.lang.String resourceBasePath)
Create a new
MockServletContext , using a DefaultResourceLoader . |
MockServletContext(java.lang.String resourceBasePath,
ResourceLoader resourceLoader)
Create a new
MockServletContext using the supplied resource base
path and resource loader. |
Modifier and Type | Method and Description |
---|---|
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
java.lang.Class<? extends Filter> filterClass) |
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
Filter filter) |
FilterRegistration.Dynamic |
addFilter(java.lang.String filterName,
java.lang.String className) |
void |
addInitParameter(java.lang.String name,
java.lang.String value) |
ServletRegistration.Dynamic |
addJspFile(java.lang.String servletName,
java.lang.String jspFile) |
void |
addListener(java.lang.Class<? extends java.util.EventListener> listenerClass) |
void |
addListener(java.lang.String className) |
<T extends java.util.EventListener> |
addListener(T t) |
void |
addMimeType(java.lang.String fileExtension,
MediaType mimeType)
Adds a mime type mapping for use by
getMimeType(String) . |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
java.lang.Class<? extends Servlet> servletClass) |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
Servlet servlet) |
ServletRegistration.Dynamic |
addServlet(java.lang.String servletName,
java.lang.String className) |
<T extends Filter> |
createFilter(java.lang.Class<T> c) |
<T extends java.util.EventListener> |
createListener(java.lang.Class<T> c) |
<T extends Servlet> |
createServlet(java.lang.Class<T> c) |
void |
declareRoles(java.lang.String... roleNames) |
java.lang.Object |
getAttribute(java.lang.String name) |
java.util.Enumeration<java.lang.String> |
getAttributeNames() |
java.lang.ClassLoader |
getClassLoader() |
ServletContext |
getContext(java.lang.String contextPath) |
java.lang.String |
getContextPath() |
java.util.Set<java.lang.String> |
getDeclaredRoles() |
java.lang.String |
getDefaultServletName()
Get the name of the default
Servlet . |
java.util.Set<SessionTrackingMode> |
getDefaultSessionTrackingModes() |
int |
getEffectiveMajorVersion() |
int |
getEffectiveMinorVersion() |
java.util.Set<SessionTrackingMode> |
getEffectiveSessionTrackingModes() |
FilterRegistration |
getFilterRegistration(java.lang.String filterName)
This method always returns
null . |
java.util.Map<java.lang.String,? extends FilterRegistration> |
getFilterRegistrations()
This method always returns an empty map.
|
java.lang.String |
getInitParameter(java.lang.String name) |
java.util.Enumeration<java.lang.String> |
getInitParameterNames() |
JspConfigDescriptor |
getJspConfigDescriptor() |
int |
getMajorVersion() |
java.lang.String |
getMimeType(java.lang.String filePath) |
int |
getMinorVersion() |
RequestDispatcher |
getNamedDispatcher(java.lang.String path) |
java.lang.String |
getRealPath(java.lang.String path) |
java.lang.String |
getRequestCharacterEncoding() |
RequestDispatcher |
getRequestDispatcher(java.lang.String path) |
java.net.URL |
getResource(java.lang.String path) |
java.io.InputStream |
getResourceAsStream(java.lang.String path) |
protected java.lang.String |
getResourceLocation(java.lang.String path)
Build a full resource location for the given path, prepending the resource
base path of this
MockServletContext . |
java.util.Set<java.lang.String> |
getResourcePaths(java.lang.String path) |
java.lang.String |
getResponseCharacterEncoding() |
java.lang.String |
getServerInfo() |
Servlet |
getServlet(java.lang.String name)
Deprecated.
|
java.lang.String |
getServletContextName() |
java.util.Enumeration<java.lang.String> |
getServletNames()
Deprecated.
|
ServletRegistration |
getServletRegistration(java.lang.String servletName)
This method always returns
null . |
java.util.Map<java.lang.String,? extends ServletRegistration> |
getServletRegistrations()
This method always returns an empty map.
|
java.util.Enumeration<Servlet> |
getServlets()
Deprecated.
|
SessionCookieConfig |
getSessionCookieConfig() |
int |
getSessionTimeout() |
java.lang.String |
getVirtualServerName() |
void |
log(java.lang.Exception ex,
java.lang.String message)
Deprecated.
|
void |
log(java.lang.String message) |
void |
log(java.lang.String message,
java.lang.Throwable ex) |
void |
registerContext(java.lang.String contextPath,
ServletContext context) |
void |
registerNamedDispatcher(java.lang.String name,
RequestDispatcher requestDispatcher)
Register a
RequestDispatcher (typically a MockRequestDispatcher )
that acts as a wrapper for the named Servlet. |
void |
removeAttribute(java.lang.String name) |
void |
setAttribute(java.lang.String name,
java.lang.Object value) |
void |
setContextPath(java.lang.String contextPath) |
void |
setDefaultServletName(java.lang.String defaultServletName)
Set the name of the default
Servlet . |
void |
setEffectiveMajorVersion(int effectiveMajorVersion) |
void |
setEffectiveMinorVersion(int effectiveMinorVersion) |
boolean |
setInitParameter(java.lang.String name,
java.lang.String value) |
void |
setMajorVersion(int majorVersion) |
void |
setMinorVersion(int minorVersion) |
void |
setRequestCharacterEncoding(java.lang.String requestCharacterEncoding) |
void |
setResponseCharacterEncoding(java.lang.String responseCharacterEncoding) |
void |
setServletContextName(java.lang.String servletContextName) |
void |
setSessionTimeout(int sessionTimeout) |
void |
setSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes) |
void |
unregisterNamedDispatcher(java.lang.String name)
Unregister the
RequestDispatcher with the given name. |
public MockServletContext()
MockServletContext
, using no base path and a
DefaultResourceLoader
(i.e. the classpath root as WAR root).DefaultResourceLoader
public MockServletContext(java.lang.String resourceBasePath)
MockServletContext
, using a DefaultResourceLoader
.resourceBasePath
- the root directory of the WAR (should not end with a slash)DefaultResourceLoader
public 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(java.lang.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 java.lang.String getResourceLocation(java.lang.String path)
MockServletContext
.path
- the path as specifiedpublic void setContextPath(java.lang.String contextPath)
public java.lang.String getContextPath()
getContextPath
in interface ServletContext
public void registerContext(java.lang.String contextPath, ServletContext context)
public ServletContext getContext(java.lang.String contextPath)
getContext
in interface ServletContext
public void setMajorVersion(int majorVersion)
public int getMajorVersion()
getMajorVersion
in interface ServletContext
public void setMinorVersion(int minorVersion)
public int getMinorVersion()
getMinorVersion
in interface ServletContext
public void setEffectiveMajorVersion(int effectiveMajorVersion)
public int getEffectiveMajorVersion()
getEffectiveMajorVersion
in interface ServletContext
public void setEffectiveMinorVersion(int effectiveMinorVersion)
public int getEffectiveMinorVersion()
getEffectiveMinorVersion
in interface ServletContext
@Nullable public java.lang.String getMimeType(java.lang.String filePath)
getMimeType
in interface ServletContext
public void addMimeType(java.lang.String fileExtension, MediaType mimeType)
getMimeType(String)
.fileExtension
- a file extension, such as txt
, gif
mimeType
- the mime type@Nullable public java.util.Set<java.lang.String> getResourcePaths(java.lang.String path)
getResourcePaths
in interface ServletContext
@Nullable public java.net.URL getResource(java.lang.String path) throws java.net.MalformedURLException
getResource
in interface ServletContext
java.net.MalformedURLException
@Nullable public java.io.InputStream getResourceAsStream(java.lang.String path)
getResourceAsStream
in interface ServletContext
public RequestDispatcher getRequestDispatcher(java.lang.String path)
getRequestDispatcher
in interface ServletContext
public RequestDispatcher getNamedDispatcher(java.lang.String path)
getNamedDispatcher
in interface ServletContext
public void registerNamedDispatcher(java.lang.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(java.lang.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 java.lang.String getDefaultServletName()
Servlet
.
Defaults to 'default'.
setDefaultServletName(java.lang.String)
public void setDefaultServletName(java.lang.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(java.lang.String name)
getServlet
in interface ServletContext
@Deprecated public java.util.Enumeration<Servlet> getServlets()
getServlets
in interface ServletContext
@Deprecated public java.util.Enumeration<java.lang.String> getServletNames()
getServletNames
in interface ServletContext
public void log(java.lang.String message)
log
in interface ServletContext
@Deprecated public void log(java.lang.Exception ex, java.lang.String message)
log
in interface ServletContext
public void log(java.lang.String message, java.lang.Throwable ex)
log
in interface ServletContext
@Nullable public java.lang.String getRealPath(java.lang.String path)
getRealPath
in interface ServletContext
public java.lang.String getServerInfo()
getServerInfo
in interface ServletContext
public java.lang.String getInitParameter(java.lang.String name)
getInitParameter
in interface ServletContext
public java.util.Enumeration<java.lang.String> getInitParameterNames()
getInitParameterNames
in interface ServletContext
public boolean setInitParameter(java.lang.String name, java.lang.String value)
setInitParameter
in interface ServletContext
public void addInitParameter(java.lang.String name, java.lang.String value)
@Nullable public java.lang.Object getAttribute(java.lang.String name)
getAttribute
in interface ServletContext
public java.util.Enumeration<java.lang.String> getAttributeNames()
getAttributeNames
in interface ServletContext
public void setAttribute(java.lang.String name, @Nullable java.lang.Object value)
setAttribute
in interface ServletContext
public void removeAttribute(java.lang.String name)
removeAttribute
in interface ServletContext
public void setServletContextName(java.lang.String servletContextName)
public java.lang.String getServletContextName()
getServletContextName
in interface ServletContext
@Nullable public java.lang.ClassLoader getClassLoader()
getClassLoader
in interface ServletContext
public void declareRoles(java.lang.String... roleNames)
declareRoles
in interface ServletContext
public java.util.Set<java.lang.String> getDeclaredRoles()
public void setSessionTrackingModes(java.util.Set<SessionTrackingMode> sessionTrackingModes) throws java.lang.IllegalStateException, java.lang.IllegalArgumentException
setSessionTrackingModes
in interface ServletContext
java.lang.IllegalStateException
java.lang.IllegalArgumentException
public java.util.Set<SessionTrackingMode> getDefaultSessionTrackingModes()
getDefaultSessionTrackingModes
in interface ServletContext
public java.util.Set<SessionTrackingMode> getEffectiveSessionTrackingModes()
getEffectiveSessionTrackingModes
in interface ServletContext
public SessionCookieConfig getSessionCookieConfig()
getSessionCookieConfig
in interface ServletContext
public void setSessionTimeout(int sessionTimeout)
public int getSessionTimeout()
public void setRequestCharacterEncoding(@Nullable java.lang.String requestCharacterEncoding)
@Nullable public java.lang.String getRequestCharacterEncoding()
public void setResponseCharacterEncoding(@Nullable java.lang.String responseCharacterEncoding)
@Nullable public java.lang.String getResponseCharacterEncoding()
public JspConfigDescriptor getJspConfigDescriptor()
getJspConfigDescriptor
in interface ServletContext
public ServletRegistration.Dynamic addJspFile(java.lang.String servletName, java.lang.String jspFile)
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.String className)
addServlet
in interface ServletContext
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, Servlet servlet)
addServlet
in interface ServletContext
public ServletRegistration.Dynamic addServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> servletClass)
addServlet
in interface ServletContext
public <T extends Servlet> T createServlet(java.lang.Class<T> c) throws ServletException
createServlet
in interface ServletContext
ServletException
@Nullable public ServletRegistration getServletRegistration(java.lang.String servletName)
null
.getServletRegistration
in interface ServletContext
ServletContext.getServletRegistration(java.lang.String)
public java.util.Map<java.lang.String,? extends ServletRegistration> getServletRegistrations()
getServletRegistrations
in interface ServletContext
ServletContext.getServletRegistrations()
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.String className)
addFilter
in interface ServletContext
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, Filter filter)
addFilter
in interface ServletContext
public FilterRegistration.Dynamic addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
addFilter
in interface ServletContext
public <T extends Filter> T createFilter(java.lang.Class<T> c) throws ServletException
createFilter
in interface ServletContext
ServletException
@Nullable public FilterRegistration getFilterRegistration(java.lang.String filterName)
null
.getFilterRegistration
in interface ServletContext
ServletContext.getFilterRegistration(java.lang.String)
public java.util.Map<java.lang.String,? extends FilterRegistration> getFilterRegistrations()
getFilterRegistrations
in interface ServletContext
ServletContext.getFilterRegistrations()
public void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
addListener
in interface ServletContext
public void addListener(java.lang.String className)
addListener
in interface ServletContext
public <T extends java.util.EventListener> void addListener(T t)
addListener
in interface ServletContext
public <T extends java.util.EventListener> T createListener(java.lang.Class<T> c) throws ServletException
createListener
in interface ServletContext
ServletException
public java.lang.String getVirtualServerName()
getVirtualServerName
in interface ServletContext