Class MockServletContext
- All Implemented Interfaces:
ServletContext
ServletContext interface.
As of Spring 6.0, this set of mocks is designed on a Servlet 6.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
ConstructorsConstructorDescriptionCreate a newMockServletContext, using no base path and aDefaultResourceLoader(i.e.MockServletContext(@Nullable ResourceLoader resourceLoader) Create a newMockServletContext, using the specifiedResourceLoaderand no base path.MockServletContext(String resourceBasePath) Create a newMockServletContext, using aDefaultResourceLoader.MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader) Create a newMockServletContextusing the supplied resource base path and resource loader. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFilterRegistration(FilterRegistration registration) Add aFilterRegistration.voidaddInitParameter(String name, String value) addJspFile(String servletName, String jspFile) voidaddListener(Class<? extends EventListener> listenerClass) voidaddListener(String className) <T extends EventListener>
voidaddListener(T t) voidaddMimeType(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) voiddeclareRoles(String... roleNames) getAttribute(String name) getContext(String contextPath) Get the name of the defaultServlet.intintgetFilterRegistration(String filterName) Map<String,? extends FilterRegistration> getInitParameter(String name) intgetMimeType(String filePath) intgetNamedDispatcher(String path) getRealPath(String path) getRequestDispatcher(String path) getResource(String path) getResourceAsStream(String path) protected StringgetResourceLocation(String path) Build a full resource location for the given path, prepending the resource base path of thisMockServletContext.getResourcePaths(String path) getServletRegistration(String servletName) This method always returnsnull.Map<String,? extends ServletRegistration> This method always returns an empty map.intvoidvoidvoidregisterContext(String contextPath, ServletContext context) voidregisterNamedDispatcher(String name, RequestDispatcher requestDispatcher) Register aRequestDispatcher(typically aMockRequestDispatcher) that acts as a wrapper for the named Servlet.voidremoveAttribute(String name) voidsetAttribute(String name, @Nullable Object value) voidsetContextPath(String contextPath) voidsetDefaultServletName(String defaultServletName) Set the name of the defaultServlet.voidsetEffectiveMajorVersion(int effectiveMajorVersion) voidsetEffectiveMinorVersion(int effectiveMinorVersion) booleansetInitParameter(String name, String value) voidsetMajorVersion(int majorVersion) voidsetMinorVersion(int minorVersion) voidsetRequestCharacterEncoding(@Nullable String requestCharacterEncoding) voidsetResponseCharacterEncoding(@Nullable String responseCharacterEncoding) voidsetServletContextName(String servletContextName) voidsetSessionTimeout(int sessionTimeout) voidsetSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) voidUnregister theRequestDispatcherwith the given name.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.ServletContext
setRequestCharacterEncoding, setResponseCharacterEncoding
-
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 specifiedResourceLoaderand no base path.- Parameters:
resourceLoader- the ResourceLoader to use (or null for the default)
-
MockServletContext
Create a newMockServletContextusing the supplied resource base path and resource loader.Registers a
MockRequestDispatcherfor 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:
getContextPathin interfaceServletContext
-
registerContext
-
getContext
- Specified by:
getContextin interfaceServletContext
-
setMajorVersion
public void setMajorVersion(int majorVersion) -
getMajorVersion
public int getMajorVersion()- Specified by:
getMajorVersionin interfaceServletContext
-
setMinorVersion
public void setMinorVersion(int minorVersion) -
getMinorVersion
public int getMinorVersion()- Specified by:
getMinorVersionin interfaceServletContext
-
setEffectiveMajorVersion
public void setEffectiveMajorVersion(int effectiveMajorVersion) -
getEffectiveMajorVersion
public int getEffectiveMajorVersion()- Specified by:
getEffectiveMajorVersionin interfaceServletContext
-
setEffectiveMinorVersion
public void setEffectiveMinorVersion(int effectiveMinorVersion) -
getEffectiveMinorVersion
public int getEffectiveMinorVersion()- Specified by:
getEffectiveMinorVersionin interfaceServletContext
-
getMimeType
- Specified by:
getMimeTypein interfaceServletContext
-
addMimeType
Adds a mime type mapping for use bygetMimeType(String).- Parameters:
fileExtension- a file extension, such astxt,gifmimeType- the mime type
-
getResourcePaths
- Specified by:
getResourcePathsin interfaceServletContext
-
getResource
- Specified by:
getResourcein interfaceServletContext- Throws:
MalformedURLException
-
getResourceAsStream
- Specified by:
getResourceAsStreamin interfaceServletContext
-
getRequestDispatcher
- Specified by:
getRequestDispatcherin interfaceServletContext
-
getNamedDispatcher
- Specified by:
getNamedDispatcherin 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 theRequestDispatcherwith 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
unregistersthe current defaultRequestDispatcherandreplacesit with aMockRequestDispatcherfor the provideddefaultServletName.- Parameters:
defaultServletName- the name of the defaultServlet; nevernullor empty- See Also:
-
log
- Specified by:
login interfaceServletContext
-
log
- Specified by:
login interfaceServletContext
-
getRealPath
- Specified by:
getRealPathin interfaceServletContext
-
getServerInfo
- Specified by:
getServerInfoin interfaceServletContext
-
getInitParameter
- Specified by:
getInitParameterin interfaceServletContext
-
getInitParameterNames
- Specified by:
getInitParameterNamesin interfaceServletContext
-
setInitParameter
- Specified by:
setInitParameterin interfaceServletContext
-
addInitParameter
-
getAttribute
- Specified by:
getAttributein interfaceServletContext
-
getAttributeNames
- Specified by:
getAttributeNamesin interfaceServletContext
-
setAttribute
- Specified by:
setAttributein interfaceServletContext
-
removeAttribute
- Specified by:
removeAttributein interfaceServletContext
-
setServletContextName
-
getServletContextName
- Specified by:
getServletContextNamein interfaceServletContext
-
getClassLoader
- Specified by:
getClassLoaderin interfaceServletContext
-
declareRoles
- Specified by:
declareRolesin interfaceServletContext
-
getDeclaredRoles
-
setSessionTrackingModes
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException - Specified by:
setSessionTrackingModesin interfaceServletContext- Throws:
IllegalStateExceptionIllegalArgumentException
-
getDefaultSessionTrackingModes
- Specified by:
getDefaultSessionTrackingModesin interfaceServletContext
-
getEffectiveSessionTrackingModes
- Specified by:
getEffectiveSessionTrackingModesin interfaceServletContext
-
getSessionCookieConfig
- Specified by:
getSessionCookieConfigin interfaceServletContext
-
setSessionTimeout
public void setSessionTimeout(int sessionTimeout) - Specified by:
setSessionTimeoutin interfaceServletContext
-
getSessionTimeout
public int getSessionTimeout()- Specified by:
getSessionTimeoutin interfaceServletContext
-
setRequestCharacterEncoding
- Specified by:
setRequestCharacterEncodingin interfaceServletContext
-
getRequestCharacterEncoding
- Specified by:
getRequestCharacterEncodingin interfaceServletContext
-
setResponseCharacterEncoding
- Specified by:
setResponseCharacterEncodingin interfaceServletContext
-
getResponseCharacterEncoding
- Specified by:
getResponseCharacterEncodingin interfaceServletContext
-
addFilterRegistration
Add aFilterRegistration.- Since:
- 6.2
-
getFilterRegistration
- Specified by:
getFilterRegistrationin interfaceServletContext
-
getFilterRegistrations
- Specified by:
getFilterRegistrationsin interfaceServletContext
-
getJspConfigDescriptor
- Specified by:
getJspConfigDescriptorin interfaceServletContext
-
addJspFile
- Specified by:
addJspFilein interfaceServletContext
-
addServlet
- Specified by:
addServletin interfaceServletContext
-
addServlet
- Specified by:
addServletin interfaceServletContext
-
addServlet
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) - Specified by:
addServletin interfaceServletContext
-
createServlet
- Specified by:
createServletin interfaceServletContext- Throws:
ServletException
-
getServletRegistration
This method always returnsnull.- Specified by:
getServletRegistrationin interfaceServletContext- See Also:
-
getServletRegistrations
This method always returns an empty map.- Specified by:
getServletRegistrationsin interfaceServletContext- See Also:
-
addFilter
- Specified by:
addFilterin interfaceServletContext
-
addFilter
- Specified by:
addFilterin interfaceServletContext
-
addFilter
- Specified by:
addFilterin interfaceServletContext
-
createFilter
- Specified by:
createFilterin interfaceServletContext- Throws:
ServletException
-
addListener
- Specified by:
addListenerin interfaceServletContext
-
addListener
- Specified by:
addListenerin interfaceServletContext
-
addListener
- Specified by:
addListenerin interfaceServletContext
-
createListener
- Specified by:
createListenerin interfaceServletContext- Throws:
ServletException
-
getVirtualServerName
- Specified by:
getVirtualServerNamein interfaceServletContext
-