Class MockServletContext
- All Implemented Interfaces:
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.
- Since:
- 1.0.2
- Author:
- Rod Johnson, Juergen Hoeller, Sam Brannen
- See Also:
-
Field Summary
Fields inherited from interface jakarta.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
-
Constructor Summary
ConstructorDescriptionCreate a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e.MockServletContext
(String resourceBasePath) Create a newMockServletContext
, using aDefaultResourceLoader
.MockServletContext
(String resourceBasePath, ResourceLoader resourceLoader) Create a newMockServletContext
using the supplied resource base path and resource loader.MockServletContext
(ResourceLoader resourceLoader) Create a newMockServletContext
, using the specifiedResourceLoader
and no base path. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addInitParameter
(String name, String value) addJspFile
(String servletName, String jspFile) void
addListener
(Class<? extends EventListener> listenerClass) void
addListener
(String className) <T extends EventListener>
voidaddListener
(T t) void
addMimeType
(String fileExtension, MediaType mimeType) Adds a mime type mapping for use bygetMimeType(String)
.addServlet
(String servletName, Servlet servlet) addServlet
(String servletName, Class<? extends Servlet> servletClass) addServlet
(String servletName, String className) <T extends Filter>
TcreateFilter
(Class<T> c) <T extends EventListener>
TcreateListener
(Class<T> c) <T extends Servlet>
TcreateServlet
(Class<T> c) void
declareRoles
(String... roleNames) getAttribute
(String name) getContext
(String contextPath) Get the name of the defaultServlet
.int
int
getFilterRegistration
(String filterName) This method always returnsnull
.Map<String,
? extends FilterRegistration> This method always returns an empty map.getInitParameter
(String name) int
getMimeType
(String filePath) int
getNamedDispatcher
(String path) getRealPath
(String path) getRequestDispatcher
(String path) getResource
(String path) getResourceAsStream
(String path) protected String
getResourceLocation
(String path) Build a full resource location for the given path, prepending the resource base path of thisMockServletContext
.getResourcePaths
(String path) getServlet
(String name) Deprecated.Deprecated.getServletRegistration
(String servletName) This method always returnsnull
.Map<String,
? extends ServletRegistration> This method always returns an empty map.Deprecated.int
void
Deprecated.void
void
void
registerContext
(String contextPath, ServletContext context) void
registerNamedDispatcher
(String name, RequestDispatcher requestDispatcher) Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.void
removeAttribute
(String name) void
setAttribute
(String name, Object value) void
setContextPath
(String contextPath) void
setDefaultServletName
(String defaultServletName) Set the name of the defaultServlet
.void
setEffectiveMajorVersion
(int effectiveMajorVersion) void
setEffectiveMinorVersion
(int effectiveMinorVersion) boolean
setInitParameter
(String name, String value) void
setMajorVersion
(int majorVersion) void
setMinorVersion
(int minorVersion) void
setRequestCharacterEncoding
(String requestCharacterEncoding) void
setResponseCharacterEncoding
(String responseCharacterEncoding) void
setServletContextName
(String servletContextName) void
setSessionTimeout
(int sessionTimeout) void
setSessionTrackingModes
(Set<SessionTrackingMode> sessionTrackingModes) void
Unregister theRequestDispatcher
with the given name.
-
Constructor Details
-
MockServletContext
public MockServletContext()Create a newMockServletContext
, using no base path and aDefaultResourceLoader
(i.e. the classpath root as WAR root).- See Also:
-
MockServletContext
Create a newMockServletContext
, using aDefaultResourceLoader
.- Parameters:
resourceBasePath
- the root directory of the WAR (should not end with a slash)- See Also:
-
MockServletContext
Create a newMockServletContext
, using the specifiedResourceLoader
and no base path.- Parameters:
resourceLoader
- the ResourceLoader to use (or null for the default)
-
MockServletContext
Create a newMockServletContext
using the supplied resource base path and resource loader.Registers a
MockRequestDispatcher
for the Servlet named 'default'.- Parameters:
resourceBasePath
- the root directory of the WAR (should not end with a slash)resourceLoader
- the ResourceLoader to use (or null for the default)- See Also:
-
-
Method Details
-
getResourceLocation
Build a full resource location for the given path, prepending the resource base path of thisMockServletContext
.- Parameters:
path
- the path as specified- Returns:
- the full resource path
-
setContextPath
-
getContextPath
- Specified by:
getContextPath
in interfaceServletContext
-
registerContext
-
getContext
- Specified by:
getContext
in interfaceServletContext
-
setMajorVersion
public void setMajorVersion(int majorVersion) -
getMajorVersion
public int getMajorVersion()- Specified by:
getMajorVersion
in interfaceServletContext
-
setMinorVersion
public void setMinorVersion(int minorVersion) -
getMinorVersion
public int getMinorVersion()- Specified by:
getMinorVersion
in interfaceServletContext
-
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion) -
getEffectiveMajorVersion
public int getEffectiveMajorVersion()- Specified by:
getEffectiveMajorVersion
in interfaceServletContext
-
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion) -
getEffectiveMinorVersion
public int getEffectiveMinorVersion()- Specified by:
getEffectiveMinorVersion
in interfaceServletContext
-
getMimeType
- Specified by:
getMimeType
in interfaceServletContext
-
addMimeType
Adds a mime type mapping for use bygetMimeType(String)
.- Parameters:
fileExtension
- a file extension, such astxt
,gif
mimeType
- the mime type
-
getResourcePaths
- Specified by:
getResourcePaths
in interfaceServletContext
-
getResource
- Specified by:
getResource
in interfaceServletContext
- Throws:
MalformedURLException
-
getResourceAsStream
- Specified by:
getResourceAsStream
in interfaceServletContext
-
getRequestDispatcher
- Specified by:
getRequestDispatcher
in interfaceServletContext
-
getNamedDispatcher
- Specified by:
getNamedDispatcher
in interfaceServletContext
-
registerNamedDispatcher
Register aRequestDispatcher
(typically aMockRequestDispatcher
) that acts as a wrapper for the named Servlet.- Parameters:
name
- the name of the wrapped ServletrequestDispatcher
- the dispatcher that wraps the named Servlet- See Also:
-
unregisterNamedDispatcher
Unregister theRequestDispatcher
with the given name.- Parameters:
name
- the name of the dispatcher to unregister- See Also:
-
getDefaultServletName
Get the name of the defaultServlet
.Defaults to 'default'.
- See Also:
-
setDefaultServletName
Set the name of the defaultServlet
.Also
unregisters
the current defaultRequestDispatcher
andreplaces
it with aMockRequestDispatcher
for the provideddefaultServletName
.- Parameters:
defaultServletName
- the name of the defaultServlet
; nevernull
or empty- See Also:
-
getServlet
Deprecated.- Specified by:
getServlet
in interfaceServletContext
-
getServlets
Deprecated.- Specified by:
getServlets
in interfaceServletContext
-
getServletNames
Deprecated.- Specified by:
getServletNames
in interfaceServletContext
-
log
- Specified by:
log
in interfaceServletContext
-
log
Deprecated.- Specified by:
log
in interfaceServletContext
-
log
- Specified by:
log
in interfaceServletContext
-
getRealPath
- Specified by:
getRealPath
in interfaceServletContext
-
getServerInfo
- Specified by:
getServerInfo
in interfaceServletContext
-
getInitParameter
- Specified by:
getInitParameter
in interfaceServletContext
-
getInitParameterNames
- Specified by:
getInitParameterNames
in interfaceServletContext
-
setInitParameter
- Specified by:
setInitParameter
in interfaceServletContext
-
addInitParameter
-
getAttribute
- Specified by:
getAttribute
in interfaceServletContext
-
getAttributeNames
- Specified by:
getAttributeNames
in interfaceServletContext
-
setAttribute
- Specified by:
setAttribute
in interfaceServletContext
-
removeAttribute
- Specified by:
removeAttribute
in interfaceServletContext
-
setServletContextName
-
getServletContextName
- Specified by:
getServletContextName
in interfaceServletContext
-
getClassLoader
- Specified by:
getClassLoader
in interfaceServletContext
-
declareRoles
- Specified by:
declareRoles
in interfaceServletContext
-
getDeclaredRoles
-
setSessionTrackingModes
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException - Specified by:
setSessionTrackingModes
in interfaceServletContext
- Throws:
IllegalStateException
IllegalArgumentException
-
getDefaultSessionTrackingModes
- Specified by:
getDefaultSessionTrackingModes
in interfaceServletContext
-
getEffectiveSessionTrackingModes
- Specified by:
getEffectiveSessionTrackingModes
in interfaceServletContext
-
getSessionCookieConfig
- Specified by:
getSessionCookieConfig
in interfaceServletContext
-
setSessionTimeout
public void setSessionTimeout(int sessionTimeout) - Specified by:
setSessionTimeout
in interfaceServletContext
-
getSessionTimeout
public int getSessionTimeout()- Specified by:
getSessionTimeout
in interfaceServletContext
-
setRequestCharacterEncoding
- Specified by:
setRequestCharacterEncoding
in interfaceServletContext
-
getRequestCharacterEncoding
- Specified by:
getRequestCharacterEncoding
in interfaceServletContext
-
setResponseCharacterEncoding
- Specified by:
setResponseCharacterEncoding
in interfaceServletContext
-
getResponseCharacterEncoding
- Specified by:
getResponseCharacterEncoding
in interfaceServletContext
-
getJspConfigDescriptor
- Specified by:
getJspConfigDescriptor
in interfaceServletContext
-
addJspFile
- Specified by:
addJspFile
in interfaceServletContext
-
addServlet
- Specified by:
addServlet
in interfaceServletContext
-
addServlet
- Specified by:
addServlet
in interfaceServletContext
-
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) - Specified by:
addServlet
in interfaceServletContext
-
createServlet
- Specified by:
createServlet
in interfaceServletContext
- Throws:
ServletException
-
getServletRegistration
This method always returnsnull
.- Specified by:
getServletRegistration
in interfaceServletContext
- See Also:
-
getServletRegistrations
This method always returns an empty map.- Specified by:
getServletRegistrations
in interfaceServletContext
- See Also:
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
addFilter
- Specified by:
addFilter
in interfaceServletContext
-
createFilter
- Specified by:
createFilter
in interfaceServletContext
- Throws:
ServletException
-
getFilterRegistration
This method always returnsnull
.- Specified by:
getFilterRegistration
in interfaceServletContext
- See Also:
-
getFilterRegistrations
This method always returns an empty map.- Specified by:
getFilterRegistrations
in interfaceServletContext
- See Also:
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
addListener
- Specified by:
addListener
in interfaceServletContext
-
createListener
- Specified by:
createListener
in interfaceServletContext
- Throws:
ServletException
-
getVirtualServerName
- Specified by:
getVirtualServerName
in interfaceServletContext
-