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 SummaryFields inherited from interface jakarta.servlet.ServletContextORDERED_LIBS, TEMPDIR
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newMockServletContext, using no base path and aDefaultResourceLoader(i.e.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.MockServletContext(@Nullable ResourceLoader resourceLoader) Create a newMockServletContext, using the specifiedResourceLoaderand no base path.
- 
Method SummaryModifier 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.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.ServletContextsetRequestCharacterEncoding, setResponseCharacterEncoding
- 
Constructor Details- 
MockServletContextpublic MockServletContext()Create a newMockServletContext, using no base path and aDefaultResourceLoader(i.e. the classpath root as WAR root).- See Also:
 
- 
MockServletContextCreate a newMockServletContext, using aDefaultResourceLoader.- Parameters:
- resourceBasePath- the root directory of the WAR (should not end with a slash)
- See Also:
 
- 
MockServletContextCreate a newMockServletContext, using the specifiedResourceLoaderand no base path.- Parameters:
- resourceLoader- the ResourceLoader to use (or null for the default)
 
- 
MockServletContextCreate 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
- 
setContextPath
- 
getContextPath- Specified by:
- getContextPathin interface- ServletContext
 
- 
registerContext
- 
getContext- Specified by:
- getContextin interface- ServletContext
 
- 
setMajorVersionpublic void setMajorVersion(int majorVersion) 
- 
getMajorVersionpublic int getMajorVersion()- Specified by:
- getMajorVersionin interface- ServletContext
 
- 
setMinorVersionpublic void setMinorVersion(int minorVersion) 
- 
getMinorVersionpublic int getMinorVersion()- Specified by:
- getMinorVersionin interface- ServletContext
 
- 
setEffectiveMajorVersionpublic void setEffectiveMajorVersion(int effectiveMajorVersion) 
- 
getEffectiveMajorVersionpublic int getEffectiveMajorVersion()- Specified by:
- getEffectiveMajorVersionin interface- ServletContext
 
- 
setEffectiveMinorVersionpublic void setEffectiveMinorVersion(int effectiveMinorVersion) 
- 
getEffectiveMinorVersionpublic int getEffectiveMinorVersion()- Specified by:
- getEffectiveMinorVersionin interface- ServletContext
 
- 
getMimeType- Specified by:
- getMimeTypein interface- ServletContext
 
- 
addMimeTypeAdds a mime type mapping for use bygetMimeType(String).- Parameters:
- fileExtension- a file extension, such as- txt,- gif
- mimeType- the mime type
 
- 
getResourcePaths- Specified by:
- getResourcePathsin interface- ServletContext
 
- 
getResource- Specified by:
- getResourcein interface- ServletContext
- Throws:
- MalformedURLException
 
- 
getResourceAsStream- Specified by:
- getResourceAsStreamin interface- ServletContext
 
- 
getRequestDispatcher- Specified by:
- getRequestDispatcherin interface- ServletContext
 
- 
getNamedDispatcher- Specified by:
- getNamedDispatcherin interface- ServletContext
 
- 
registerNamedDispatcherRegister aRequestDispatcher(typically aMockRequestDispatcher) that acts as a wrapper for the named Servlet.- Parameters:
- name- the name of the wrapped Servlet
- requestDispatcher- the dispatcher that wraps the named Servlet
- See Also:
 
- 
unregisterNamedDispatcherUnregister theRequestDispatcherwith the given name.- Parameters:
- name- the name of the dispatcher to unregister
- See Also:
 
- 
getDefaultServletNameGet the name of the defaultServlet.Defaults to 'default'. - See Also:
 
- 
setDefaultServletNameSet the name of the defaultServlet.Also unregistersthe current defaultRequestDispatcherandreplacesit with aMockRequestDispatcherfor the provideddefaultServletName.- Parameters:
- defaultServletName- the name of the default- Servlet; never- nullor empty
- See Also:
 
- 
log- Specified by:
- login interface- ServletContext
 
- 
log- Specified by:
- login interface- ServletContext
 
- 
getRealPath- Specified by:
- getRealPathin interface- ServletContext
 
- 
getServerInfo- Specified by:
- getServerInfoin interface- ServletContext
 
- 
getInitParameter- Specified by:
- getInitParameterin interface- ServletContext
 
- 
getInitParameterNames- Specified by:
- getInitParameterNamesin interface- ServletContext
 
- 
setInitParameter- Specified by:
- setInitParameterin interface- ServletContext
 
- 
addInitParameter
- 
getAttribute- Specified by:
- getAttributein interface- ServletContext
 
- 
getAttributeNames- Specified by:
- getAttributeNamesin interface- ServletContext
 
- 
setAttribute- Specified by:
- setAttributein interface- ServletContext
 
- 
removeAttribute- Specified by:
- removeAttributein interface- ServletContext
 
- 
setServletContextName
- 
getServletContextName- Specified by:
- getServletContextNamein interface- ServletContext
 
- 
getClassLoader- Specified by:
- getClassLoaderin interface- ServletContext
 
- 
declareRoles- Specified by:
- declareRolesin interface- ServletContext
 
- 
getDeclaredRoles
- 
setSessionTrackingModespublic void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException - Specified by:
- setSessionTrackingModesin interface- ServletContext
- Throws:
- IllegalStateException
- IllegalArgumentException
 
- 
getDefaultSessionTrackingModes- Specified by:
- getDefaultSessionTrackingModesin interface- ServletContext
 
- 
getEffectiveSessionTrackingModes- Specified by:
- getEffectiveSessionTrackingModesin interface- ServletContext
 
- 
getSessionCookieConfig- Specified by:
- getSessionCookieConfigin interface- ServletContext
 
- 
setSessionTimeoutpublic void setSessionTimeout(int sessionTimeout) - Specified by:
- setSessionTimeoutin interface- ServletContext
 
- 
getSessionTimeoutpublic int getSessionTimeout()- Specified by:
- getSessionTimeoutin interface- ServletContext
 
- 
setRequestCharacterEncoding- Specified by:
- setRequestCharacterEncodingin interface- ServletContext
 
- 
getRequestCharacterEncoding- Specified by:
- getRequestCharacterEncodingin interface- ServletContext
 
- 
setResponseCharacterEncoding- Specified by:
- setResponseCharacterEncodingin interface- ServletContext
 
- 
getResponseCharacterEncoding- Specified by:
- getResponseCharacterEncodingin interface- ServletContext
 
- 
addFilterRegistrationAdd aFilterRegistration.- Since:
- 6.2
 
- 
getFilterRegistration- Specified by:
- getFilterRegistrationin interface- ServletContext
 
- 
getFilterRegistrations- Specified by:
- getFilterRegistrationsin interface- ServletContext
 
- 
getJspConfigDescriptor- Specified by:
- getJspConfigDescriptorin interface- ServletContext
 
- 
addJspFile- Specified by:
- addJspFilein interface- ServletContext
 
- 
addServlet- Specified by:
- addServletin interface- ServletContext
 
- 
addServlet- Specified by:
- addServletin interface- ServletContext
 
- 
addServletpublic ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) - Specified by:
- addServletin interface- ServletContext
 
- 
createServlet- Specified by:
- createServletin interface- ServletContext
- Throws:
- ServletException
 
- 
getServletRegistrationThis method always returnsnull.- Specified by:
- getServletRegistrationin interface- ServletContext
- See Also:
 
- 
getServletRegistrationsThis method always returns an empty map.- Specified by:
- getServletRegistrationsin interface- ServletContext
- See Also:
 
- 
addFilter- Specified by:
- addFilterin interface- ServletContext
 
- 
addFilter- Specified by:
- addFilterin interface- ServletContext
 
- 
addFilter- Specified by:
- addFilterin interface- ServletContext
 
- 
createFilter- Specified by:
- createFilterin interface- ServletContext
- Throws:
- ServletException
 
- 
addListener- Specified by:
- addListenerin interface- ServletContext
 
- 
addListener- Specified by:
- addListenerin interface- ServletContext
 
- 
addListener- Specified by:
- addListenerin interface- ServletContext
 
- 
createListener- Specified by:
- createListenerin interface- ServletContext
- Throws:
- ServletException
 
- 
getVirtualServerName- Specified by:
- getVirtualServerNamein interface- ServletContext
 
 
-