Class MockServletContext

java.lang.Object
org.springframework.mock.web.MockServletContext
All Implemented Interfaces:
jakarta.servlet.ServletContext

public class MockServletContext extends Object implements jakarta.servlet.ServletContext
Mock implementation of the ServletContext interface.

As of Spring 7.0, this set of mocks is designed on a Servlet 6.1 baseline.

Compatible with Servlet 6.1 but can be configured to expose a specific version through setMajorVersion(int)/setMinorVersion(int); default is 6.1. Note that some Servlet SPI 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:
  • Constructor Details

    • MockServletContext

      public MockServletContext()
      Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e. the classpath root as WAR root).
      See Also:
    • MockServletContext

      public MockServletContext(String resourceBasePath)
      Create a new MockServletContext, using a DefaultResourceLoader.
      Parameters:
      resourceBasePath - the root directory of the WAR (should not end with a slash)
      See Also:
    • MockServletContext

      public MockServletContext(@Nullable ResourceLoader resourceLoader)
      Create a new MockServletContext, using the specified ResourceLoader and no base path.
      Parameters:
      resourceLoader - the ResourceLoader to use (or null for the default)
    • MockServletContext

      public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader)
      Create a new MockServletContext 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

      protected String getResourceLocation(String path)
      Build a full resource location for the given path, prepending the resource base path of this MockServletContext.
      Parameters:
      path - the path as specified
      Returns:
      the full resource path
    • setContextPath

      public void setContextPath(String contextPath)
    • getContextPath

      public String getContextPath()
      Specified by:
      getContextPath in interface jakarta.servlet.ServletContext
    • registerContext

      public void registerContext(String contextPath, jakarta.servlet.ServletContext context)
    • getContext

      public @Nullable jakarta.servlet.ServletContext getContext(String contextPath)
      Specified by:
      getContext in interface jakarta.servlet.ServletContext
    • setMajorVersion

      public void setMajorVersion(int majorVersion)
    • getMajorVersion

      public int getMajorVersion()
      Specified by:
      getMajorVersion in interface jakarta.servlet.ServletContext
    • setMinorVersion

      public void setMinorVersion(int minorVersion)
    • getMinorVersion

      public int getMinorVersion()
      Specified by:
      getMinorVersion in interface jakarta.servlet.ServletContext
    • setEffectiveMajorVersion

      public void setEffectiveMajorVersion(int effectiveMajorVersion)
    • getEffectiveMajorVersion

      public int getEffectiveMajorVersion()
      Specified by:
      getEffectiveMajorVersion in interface jakarta.servlet.ServletContext
    • setEffectiveMinorVersion

      public void setEffectiveMinorVersion(int effectiveMinorVersion)
    • getEffectiveMinorVersion

      public int getEffectiveMinorVersion()
      Specified by:
      getEffectiveMinorVersion in interface jakarta.servlet.ServletContext
    • getMimeType

      public @Nullable String getMimeType(String filePath)
      Specified by:
      getMimeType in interface jakarta.servlet.ServletContext
    • addMimeType

      public void addMimeType(String fileExtension, MediaType mimeType)
      Adds a mime type mapping for use by getMimeType(String).
      Parameters:
      fileExtension - a file extension, such as txt, gif
      mimeType - the mime type
    • getResourcePaths

      public @Nullable Set<String> getResourcePaths(String path)
      Specified by:
      getResourcePaths in interface jakarta.servlet.ServletContext
    • getResource

      public @Nullable URL getResource(String path) throws MalformedURLException
      Specified by:
      getResource in interface jakarta.servlet.ServletContext
      Throws:
      MalformedURLException
    • getResourceAsStream

      public @Nullable InputStream getResourceAsStream(String path)
      Specified by:
      getResourceAsStream in interface jakarta.servlet.ServletContext
    • getRequestDispatcher

      public jakarta.servlet.RequestDispatcher getRequestDispatcher(String path)
      Specified by:
      getRequestDispatcher in interface jakarta.servlet.ServletContext
    • getNamedDispatcher

      public @Nullable jakarta.servlet.RequestDispatcher getNamedDispatcher(String path)
      Specified by:
      getNamedDispatcher in interface jakarta.servlet.ServletContext
    • registerNamedDispatcher

      public void registerNamedDispatcher(String name, jakarta.servlet.RequestDispatcher requestDispatcher)
      Register a RequestDispatcher (typically a MockRequestDispatcher) 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:
    • unregisterNamedDispatcher

      public void unregisterNamedDispatcher(String name)
      Unregister the RequestDispatcher with the given name.
      Parameters:
      name - the name of the dispatcher to unregister
      See Also:
    • getDefaultServletName

      public String getDefaultServletName()
      Get the name of the default Servlet.

      Defaults to 'default'.

      See Also:
    • setDefaultServletName

      public void setDefaultServletName(String defaultServletName)
      Set the name of the default Servlet.

      Also unregisters the current default RequestDispatcher and replaces it with a MockRequestDispatcher for the provided defaultServletName.

      Parameters:
      defaultServletName - the name of the default Servlet; never null or empty
      See Also:
    • log

      public void log(String message)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • log

      public void log(String message, Throwable ex)
      Specified by:
      log in interface jakarta.servlet.ServletContext
    • getRealPath

      public @Nullable String getRealPath(String path)
      Specified by:
      getRealPath in interface jakarta.servlet.ServletContext
    • getServerInfo

      public String getServerInfo()
      Specified by:
      getServerInfo in interface jakarta.servlet.ServletContext
    • getInitParameter

      public @Nullable String getInitParameter(String name)
      Specified by:
      getInitParameter in interface jakarta.servlet.ServletContext
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
      Specified by:
      getInitParameterNames in interface jakarta.servlet.ServletContext
    • setInitParameter

      public boolean setInitParameter(String name, String value)
      Specified by:
      setInitParameter in interface jakarta.servlet.ServletContext
    • addInitParameter

      public void addInitParameter(String name, String value)
    • getAttribute

      public @Nullable Object getAttribute(String name)
      Specified by:
      getAttribute in interface jakarta.servlet.ServletContext
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
      Specified by:
      getAttributeNames in interface jakarta.servlet.ServletContext
    • setAttribute

      public void setAttribute(String name, @Nullable Object value)
      Specified by:
      setAttribute in interface jakarta.servlet.ServletContext
    • removeAttribute

      public void removeAttribute(String name)
      Specified by:
      removeAttribute in interface jakarta.servlet.ServletContext
    • setServletContextName

      public void setServletContextName(String servletContextName)
    • getServletContextName

      public String getServletContextName()
      Specified by:
      getServletContextName in interface jakarta.servlet.ServletContext
    • getClassLoader

      public @Nullable ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface jakarta.servlet.ServletContext
    • declareRoles

      public void declareRoles(String... roleNames)
      Specified by:
      declareRoles in interface jakarta.servlet.ServletContext
    • getDeclaredRoles

      public Set<String> getDeclaredRoles()
    • setSessionTrackingModes

      public void setSessionTrackingModes(Set<jakarta.servlet.SessionTrackingMode> sessionTrackingModes) throws IllegalStateException, IllegalArgumentException
      Specified by:
      setSessionTrackingModes in interface jakarta.servlet.ServletContext
      Throws:
      IllegalStateException
      IllegalArgumentException
    • getDefaultSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getDefaultSessionTrackingModes()
      Specified by:
      getDefaultSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getEffectiveSessionTrackingModes

      public Set<jakarta.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes()
      Specified by:
      getEffectiveSessionTrackingModes in interface jakarta.servlet.ServletContext
    • getSessionCookieConfig

      public jakarta.servlet.SessionCookieConfig getSessionCookieConfig()
      Specified by:
      getSessionCookieConfig in interface jakarta.servlet.ServletContext
    • setSessionTimeout

      public void setSessionTimeout(int sessionTimeout)
      Specified by:
      setSessionTimeout in interface jakarta.servlet.ServletContext
    • getSessionTimeout

      public int getSessionTimeout()
      Specified by:
      getSessionTimeout in interface jakarta.servlet.ServletContext
    • setRequestCharacterEncoding

      public void setRequestCharacterEncoding(@Nullable String requestCharacterEncoding)
      Specified by:
      setRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • getRequestCharacterEncoding

      public @Nullable String getRequestCharacterEncoding()
      Specified by:
      getRequestCharacterEncoding in interface jakarta.servlet.ServletContext
    • setResponseCharacterEncoding

      public void setResponseCharacterEncoding(@Nullable String responseCharacterEncoding)
      Specified by:
      setResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • getResponseCharacterEncoding

      public @Nullable String getResponseCharacterEncoding()
      Specified by:
      getResponseCharacterEncoding in interface jakarta.servlet.ServletContext
    • addFilterRegistration

      public void addFilterRegistration(jakarta.servlet.FilterRegistration registration)
      Add a FilterRegistration.
      Since:
      6.2
    • getFilterRegistration

      public @Nullable jakarta.servlet.FilterRegistration getFilterRegistration(String filterName)
      Specified by:
      getFilterRegistration in interface jakarta.servlet.ServletContext
    • getFilterRegistrations

      public Map<String, ? extends jakarta.servlet.FilterRegistration> getFilterRegistrations()
      Specified by:
      getFilterRegistrations in interface jakarta.servlet.ServletContext
    • getJspConfigDescriptor

      public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface jakarta.servlet.ServletContext
    • addJspFile

      public jakarta.servlet.ServletRegistration.Dynamic addJspFile(String servletName, String jspFile)
      Specified by:
      addJspFile in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, jakarta.servlet.Servlet servlet)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • addServlet

      public jakarta.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
    • createServlet

      public <T extends jakarta.servlet.Servlet> T createServlet(Class<T> c) throws jakarta.servlet.ServletException
      Specified by:
      createServlet in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getServletRegistration

      public @Nullable jakarta.servlet.ServletRegistration getServletRegistration(String servletName)
      This method always returns null.
      Specified by:
      getServletRegistration in interface jakarta.servlet.ServletContext
      See Also:
      • ServletContext.getServletRegistration(java.lang.String)
    • getServletRegistrations

      public Map<String, ? extends jakarta.servlet.ServletRegistration> getServletRegistrations()
      This method always returns an empty map.
      Specified by:
      getServletRegistrations in interface jakarta.servlet.ServletContext
      See Also:
      • ServletContext.getServletRegistrations()
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, jakarta.servlet.Filter filter)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • addFilter

      public jakarta.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
    • createFilter

      public <T extends jakarta.servlet.Filter> T createFilter(Class<T> c) throws jakarta.servlet.ServletException
      Specified by:
      createFilter in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
    • createListener

      public <T extends EventListener> T createListener(Class<T> c) throws jakarta.servlet.ServletException
      Specified by:
      createListener in interface jakarta.servlet.ServletContext
      Throws:
      jakarta.servlet.ServletException
    • getVirtualServerName

      public String getVirtualServerName()
      Specified by:
      getVirtualServerName in interface jakarta.servlet.ServletContext