org.springframework.mock.web

## Class MockServletContext

- java.lang.Object
- - org.springframework.mock.web.MockServletContext

- All Implemented Interfaces:
    ServletContext

      public class MockServletContext
      extends Object
      implements ServletContext

  Mock implementation of the `ServletContext` interface.

  As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.

  Compatible with Servlet 3.0 but can be configured to expose a specific version through `setMajorVersion(int)`/`setMinorVersion(int)`; default is 3.0. Note that Servlet 3.0 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.

  Used for testing the Spring web framework; only rarely necessary for testing application controllers. As long as application components don't explicitly access the `ServletContext`, `ClassPathXmlApplicationContext` or `FileSystemXmlApplicationContext` can be used to load the context files for testing, even for `DispatcherServlet` context definitions.

  For setting up a full `WebApplicationContext` in a test environment, you can use `AnnotationConfigWebApplicationContext`, `XmlWebApplicationContext`, or `GenericWebApplicationContext`, passing in an appropriate `MockServletContext` instance. You might want to configure your `MockServletContext` with a `FileSystemResourceLoader` in that case to ensure that resource paths are interpreted as relative filesystem locations.

  A common setup is to point your JVM working directory to the root of your web application directory, in combination with filesystem-based resource loading. This allows to load the context files as used in the web application, with relative paths getting interpreted correctly. Such a setup will work with both `FileSystemXmlApplicationContext` (which will load straight from the filesystem) and `XmlWebApplicationContext` with an underlying `MockServletContext` (as long as the `MockServletContext` has been configured with a `FileSystemResourceLoader`).

  Since:
    1.0.2
  Author:
    Rod Johnson, Juergen Hoeller, Sam Brannen
  See Also:
    `MockServletContext(org.springframework.core.io.ResourceLoader)`, `AnnotationConfigWebApplicationContext`, `XmlWebApplicationContext`, `GenericWebApplicationContext`, `ClassPathXmlApplicationContext`, `FileSystemXmlApplicationContext`

- - ### Field Summary

    - ### Fields inherited from interface javax.servlet.ServletContext

      `ORDERED_LIBS, TEMPDIR`

  - ### Constructor Summary

    Constructors
    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(String resourceBasePath)` Create a new MockServletContext, using a DefaultResourceLoader.
    `MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)` Create a new MockServletContext using the supplied resource base path and resource loader.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `FilterRegistration.Dynamic` | `addFilter(String filterName, Class<? extends Filter> filterClass)`
    `FilterRegistration.Dynamic` | `addFilter(String filterName, Filter filter)`
    `FilterRegistration.Dynamic` | `addFilter(String filterName, String className)`
    `void` | `addInitParameter(String name, String value)`
    `void` | `addListener(Class<? extends EventListener> listenerClass)`
    `void` | `addListener(String className)`
    `<T extends EventListener> void` | `addListener(T t)`
    `ServletRegistration.Dynamic` | `addServlet(String servletName, Class<? extends Servlet> servletClass)`
    `ServletRegistration.Dynamic` | `addServlet(String servletName, Servlet servlet)`
    `ServletRegistration.Dynamic` | `addServlet(String servletName, String className)`
    `<T extends Filter> T` | `createFilter(Class<T> c)`
    `<T extends EventListener> T` | `createListener(Class<T> c)`
    `<T extends Servlet> T` | `createServlet(Class<T> c)`
    `void` | `declareRoles(String... roleNames)`
    `Object` | `getAttribute(String name)`
    `Enumeration<String>` | `getAttributeNames()`
    `ClassLoader` | `getClassLoader()`
    `ServletContext` | `getContext(String contextPath)`
    `String` | `getContextPath()`
    `Set<String>` | `getDeclaredRoles()`
    `String` | `getDefaultServletName()` Get the name of the default `Servlet`.
    `Set<SessionTrackingMode>` | `getDefaultSessionTrackingModes()`
    `int` | `getEffectiveMajorVersion()`
    `int` | `getEffectiveMinorVersion()`
    `Set<SessionTrackingMode>` | `getEffectiveSessionTrackingModes()`
    `FilterRegistration` | `getFilterRegistration(String filterName)`
    `Map<String,? extends FilterRegistration>` | `getFilterRegistrations()`
    `String` | `getInitParameter(String name)`
    `Enumeration<String>` | `getInitParameterNames()`
    `JspConfigDescriptor` | `getJspConfigDescriptor()`
    `int` | `getMajorVersion()`
    `String` | `getMimeType(String filePath)` This method uses the Java Activation framework, which returns "application/octet-stream" when the mime type is unknown (i.e.
    `int` | `getMinorVersion()`
    `RequestDispatcher` | `getNamedDispatcher(String path)`
    `String` | `getRealPath(String path)`
    `RequestDispatcher` | `getRequestDispatcher(String path)`
    `URL` | `getResource(String path)`
    `InputStream` | `getResourceAsStream(String path)`
    `protected String` | `getResourceLocation(String path)` Build a full resource location for the given path, prepending the resource base path of this MockServletContext.
    `Set<String>` | `getResourcePaths(String path)`
    `String` | `getServerInfo()`
    `Servlet` | `getServlet(String name)` Deprecated.
    `String` | `getServletContextName()`
    `Enumeration<String>` | `getServletNames()` Deprecated.
    `ServletRegistration` | `getServletRegistration(String servletName)`
    `Map<String,? extends ServletRegistration>` | `getServletRegistrations()`
    `Enumeration<Servlet>` | `getServlets()` Deprecated.
    `SessionCookieConfig` | `getSessionCookieConfig()`
    `void` | `log(Exception ex, String message)` Deprecated.
    `void` | `log(String message)`
    `void` | `log(String message, Throwable ex)`
    `void` | `registerContext(String contextPath, ServletContext context)`
    `void` | `registerNamedDispatcher(String name, RequestDispatcher requestDispatcher)` Register a `RequestDispatcher` (typically a `MockRequestDispatcher`) 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 default `Servlet`.
    `void` | `setEffectiveMajorVersion(int effectiveMajorVersion)`
    `void` | `setEffectiveMinorVersion(int effectiveMinorVersion)`
    `boolean` | `setInitParameter(String name, String value)`
    `void` | `setMajorVersion(int majorVersion)`
    `void` | `setMinorVersion(int minorVersion)`
    `void` | `setServletContextName(String servletContextName)`
    `void` | `setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)`
    `void` | `unregisterNamedDispatcher(String name)` Unregister the `RequestDispatcher` with the given name.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### MockServletContext

          public MockServletContext()

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

    - #### 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:
        `DefaultResourceLoader`

    - #### MockServletContext

          public MockServletContext(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,
                                    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:
        `registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)`

  - ### Method Detail

    - #### 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 `ServletContext`

    - #### registerContext

          public void registerContext(String contextPath,
                                      ServletContext context)

    - #### getContext

          public ServletContext getContext(String contextPath)

      Specified by:
        `getContext` in interface `ServletContext`

    - #### setMajorVersion

          public void setMajorVersion(int majorVersion)

    - #### getMajorVersion

          public int getMajorVersion()

      Specified by:
        `getMajorVersion` in interface `ServletContext`

    - #### setMinorVersion

          public void setMinorVersion(int minorVersion)

    - #### getMinorVersion

          public int getMinorVersion()

      Specified by:
        `getMinorVersion` in interface `ServletContext`

    - #### setEffectiveMajorVersion

          public void setEffectiveMajorVersion(int effectiveMajorVersion)

    - #### getEffectiveMajorVersion

          public int getEffectiveMajorVersion()

      Specified by:
        `getEffectiveMajorVersion` in interface `ServletContext`

    - #### setEffectiveMinorVersion

          public void setEffectiveMinorVersion(int effectiveMinorVersion)

    - #### getEffectiveMinorVersion

          public int getEffectiveMinorVersion()

      Specified by:
        `getEffectiveMinorVersion` in interface `ServletContext`

    - #### getMimeType

          public String getMimeType(String filePath)

      This method uses the Java Activation framework, which returns "application/octet-stream" when the mime type is unknown (i.e. it never returns `null`). In order to maintain the `ServletContext.getMimeType(String)` contract, this method returns `null` if the mimeType is "application/octet-stream", as of Spring 3.2.2.
      Specified by:
        `getMimeType` in interface `ServletContext`

    - #### getResourcePaths

          public Set<String> getResourcePaths(String path)

      Specified by:
        `getResourcePaths` in interface `ServletContext`

    - #### getResource

          public URL getResource(String path)
                          throws MalformedURLException

      Specified by:
        `getResource` in interface `ServletContext`
      Throws:
        `MalformedURLException`

    - #### getResourceAsStream

          public InputStream getResourceAsStream(String path)

      Specified by:
        `getResourceAsStream` in interface `ServletContext`

    - #### getRequestDispatcher

          public RequestDispatcher getRequestDispatcher(String path)

      Specified by:
        `getRequestDispatcher` in interface `ServletContext`

    - #### getNamedDispatcher

          public RequestDispatcher getNamedDispatcher(String path)

      Specified by:
        `getNamedDispatcher` in interface `ServletContext`

    - #### registerNamedDispatcher

          public void registerNamedDispatcher(String name,
                                              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:
        `getNamedDispatcher(java.lang.String)`, `unregisterNamedDispatcher(java.lang.String)`

    - #### unregisterNamedDispatcher

          public void unregisterNamedDispatcher(String name)

      Unregister the `RequestDispatcher` with the given name.
      Parameters:
        `name` - the name of the dispatcher to unregister
      See Also:
        `getNamedDispatcher(java.lang.String)`, `registerNamedDispatcher(java.lang.String, javax.servlet.RequestDispatcher)`

    - #### getDefaultServletName

          public String getDefaultServletName()

      Get the name of the default `Servlet`.

      Defaults to 'default'.

      See Also:
        `setDefaultServletName(java.lang.String)`

    - #### 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:
        `getDefaultServletName()`

    - #### getServlet

          @Deprecated
          public Servlet getServlet(String name)

      Deprecated.
      Specified by:
        `getServlet` in interface `ServletContext`

    - #### getServlets

          @Deprecated
          public Enumeration<Servlet> getServlets()

      Deprecated.
      Specified by:
        `getServlets` in interface `ServletContext`

    - #### getServletNames

          @Deprecated
          public Enumeration<String> getServletNames()

      Deprecated.
      Specified by:
        `getServletNames` in interface `ServletContext`

    - #### log

          public void log(String message)

      Specified by:
        `log` in interface `ServletContext`

    - #### log

          @Deprecated
          public void log(Exception ex,
                                      String message)

      Deprecated.
      Specified by:
        `log` in interface `ServletContext`

    - #### log

          public void log(String message,
                          Throwable ex)

      Specified by:
        `log` in interface `ServletContext`

    - #### getRealPath

          public String getRealPath(String path)

      Specified by:
        `getRealPath` in interface `ServletContext`

    - #### getServerInfo

          public String getServerInfo()

      Specified by:
        `getServerInfo` in interface `ServletContext`

    - #### getInitParameter

          public String getInitParameter(String name)

      Specified by:
        `getInitParameter` in interface `ServletContext`

    - #### getInitParameterNames

          public Enumeration<String> getInitParameterNames()

      Specified by:
        `getInitParameterNames` in interface `ServletContext`

    - #### setInitParameter

          public boolean setInitParameter(String name,
                                          String value)

      Specified by:
        `setInitParameter` in interface `ServletContext`

    - #### addInitParameter

          public void addInitParameter(String name,
                                       String value)

    - #### getAttribute

          public Object getAttribute(String name)

      Specified by:
        `getAttribute` in interface `ServletContext`

    - #### getAttributeNames

          public Enumeration<String> getAttributeNames()

      Specified by:
        `getAttributeNames` in interface `ServletContext`

    - #### setAttribute

          public void setAttribute(String name,
                                   Object value)

      Specified by:
        `setAttribute` in interface `ServletContext`

    - #### removeAttribute

          public void removeAttribute(String name)

      Specified by:
        `removeAttribute` in interface `ServletContext`

    - #### setServletContextName

          public void setServletContextName(String servletContextName)

    - #### getServletContextName

          public String getServletContextName()

      Specified by:
        `getServletContextName` in interface `ServletContext`

    - #### getClassLoader

          public ClassLoader getClassLoader()

      Specified by:
        `getClassLoader` in interface `ServletContext`

    - #### declareRoles

          public void declareRoles(String... roleNames)

      Specified by:
        `declareRoles` in interface `ServletContext`

    - #### getDeclaredRoles

          public Set<String> getDeclaredRoles()

    - #### setSessionTrackingModes

          public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
                                       throws IllegalStateException,
                                              IllegalArgumentException

      Specified by:
        `setSessionTrackingModes` in interface `ServletContext`
      Throws:
        `IllegalStateException`
        `IllegalArgumentException`

    - #### getDefaultSessionTrackingModes

          public Set<SessionTrackingMode> getDefaultSessionTrackingModes()

      Specified by:
        `getDefaultSessionTrackingModes` in interface `ServletContext`

    - #### getEffectiveSessionTrackingModes

          public Set<SessionTrackingMode> getEffectiveSessionTrackingModes()

      Specified by:
        `getEffectiveSessionTrackingModes` in interface `ServletContext`

    - #### getSessionCookieConfig

          public SessionCookieConfig getSessionCookieConfig()

      Specified by:
        `getSessionCookieConfig` in interface `ServletContext`

    - #### getJspConfigDescriptor

          public JspConfigDescriptor getJspConfigDescriptor()

      Specified by:
        `getJspConfigDescriptor` in interface `ServletContext`

    - #### addServlet

          public ServletRegistration.Dynamic addServlet(String servletName,
                                                        String className)

      Specified by:
        `addServlet` in interface `ServletContext`

    - #### addServlet

          public ServletRegistration.Dynamic addServlet(String servletName,
                                                        Servlet servlet)

      Specified by:
        `addServlet` in interface `ServletContext`

    - #### addServlet

          public ServletRegistration.Dynamic addServlet(String servletName,
                                                        Class<? extends Servlet> servletClass)

      Specified by:
        `addServlet` in interface `ServletContext`

    - #### createServlet

          public <T extends Servlet> T createServlet(Class<T> c)
                                              throws ServletException

      Specified by:
        `createServlet` in interface `ServletContext`
      Throws:
        `ServletException`

    - #### getServletRegistration

          public ServletRegistration getServletRegistration(String servletName)

      Specified by:
        `getServletRegistration` in interface `ServletContext`

    - #### getServletRegistrations

          public Map<String,? extends ServletRegistration> getServletRegistrations()

      Specified by:
        `getServletRegistrations` in interface `ServletContext`

    - #### addFilter

          public FilterRegistration.Dynamic addFilter(String filterName,
                                                      String className)

      Specified by:
        `addFilter` in interface `ServletContext`

    - #### addFilter

          public FilterRegistration.Dynamic addFilter(String filterName,
                                                      Filter filter)

      Specified by:
        `addFilter` in interface `ServletContext`

    - #### addFilter

          public FilterRegistration.Dynamic addFilter(String filterName,
                                                      Class<? extends Filter> filterClass)

      Specified by:
        `addFilter` in interface `ServletContext`

    - #### createFilter

          public <T extends Filter> T createFilter(Class<T> c)
                                            throws ServletException

      Specified by:
        `createFilter` in interface `ServletContext`
      Throws:
        `ServletException`

    - #### getFilterRegistration

          public FilterRegistration getFilterRegistration(String filterName)

      Specified by:
        `getFilterRegistration` in interface `ServletContext`

    - #### getFilterRegistrations

          public Map<String,? extends FilterRegistration> getFilterRegistrations()

      Specified by:
        `getFilterRegistrations` in interface `ServletContext`

    - #### addListener

          public void addListener(Class<? extends EventListener> listenerClass)

      Specified by:
        `addListener` in interface `ServletContext`

    - #### addListener

          public void addListener(String className)

      Specified by:
        `addListener` in interface `ServletContext`

    - #### addListener

          public <T extends EventListener> void addListener(T t)

      Specified by:
        `addListener` in interface `ServletContext`

    - #### createListener

          public <T extends EventListener> T createListener(Class<T> c)
                                                     throws ServletException

      Specified by:
        `createListener` in interface `ServletContext`
      Throws:
        `ServletException`