public class

MockServletContext

extends Object
implements ServletContext
java.lang.Object
   ↳ org.springframework.mock.web.MockServletContext

Class Overview

Mock implementation of the javax.servlet.ServletContext interface.

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 XmlWebApplicationContext (or GenericWebApplicationContext), passing in an appropriate MockServletContext instance. You might want to configure your MockServletContext with a FileSystemResourceLoader in that case, to make your resource paths interpreted as relative file system 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 file system) and XmlWebApplicationContext with an underlying MockServletContext (as long as the MockServletContext has been configured with a FileSystemResourceLoader).

Summary

Public Constructors
MockServletContext()
Create a new MockServletContext, using no base path and a DefaultResourceLoader (i.e.
MockServletContext(String resourceBasePath)
Create a new MockServletContext, using a DefaultResourceLoader.
MockServletContext(ResourceLoader resourceLoader)
Create a new MockServletContext, using the specified ResourceLoader and no base path.
MockServletContext(String resourceBasePath, ResourceLoader resourceLoader)
Create a new MockServletContext.
Public Methods
void addInitParameter(String name, String value)
Object getAttribute(String name)
Enumeration<String> getAttributeNames()
ServletContext getContext(String contextPath)
String getContextPath()
String getInitParameter(String name)
Enumeration<String> getInitParameterNames()
int getMajorVersion()
String getMimeType(String filePath)
int getMinorVersion()
RequestDispatcher getNamedDispatcher(String path)
String getRealPath(String path)
RequestDispatcher getRequestDispatcher(String path)
URL getResource(String path)
InputStream getResourceAsStream(String path)
Set<String> getResourcePaths(String path)
String getServerInfo()
Servlet getServlet(String name)
String getServletContextName()
Enumeration<String> getServletNames()
Enumeration<Servlet> getServlets()
void log(String message, Throwable ex)
void log(Exception ex, String message)
void log(String message)
void registerContext(String contextPath, ServletContext context)
void removeAttribute(String name)
void setAttribute(String name, Object value)
void setContextPath(String contextPath)
void setMinorVersion(int minorVersion)
void setServletContextName(String servletContextName)
Protected Methods
String getResourceLocation(String path)
Build a full resource location for the given path, prepending the resource base path of this MockServletContext.
[Expand]
Inherited Methods
From class java.lang.Object
From interface javax.servlet.ServletContext

Public Constructors

public MockServletContext ()

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

public MockServletContext (String resourceBasePath)

Create a new MockServletContext, using a DefaultResourceLoader.

Parameters
resourceBasePath the WAR root directory (should not end with a slash)

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)

public MockServletContext (String resourceBasePath, ResourceLoader resourceLoader)

Create a new MockServletContext.

Parameters
resourceBasePath the WAR root directory (should not end with a slash)
resourceLoader the ResourceLoader to use (or null for the default)

Public Methods

public void addInitParameter (String name, String value)

public Object getAttribute (String name)

public Enumeration<String> getAttributeNames ()

public ServletContext getContext (String contextPath)

public String getContextPath ()

public String getInitParameter (String name)

public Enumeration<String> getInitParameterNames ()

public int getMajorVersion ()

public String getMimeType (String filePath)

public int getMinorVersion ()

public RequestDispatcher getNamedDispatcher (String path)

public String getRealPath (String path)

public RequestDispatcher getRequestDispatcher (String path)

public URL getResource (String path)

public InputStream getResourceAsStream (String path)

public Set<String> getResourcePaths (String path)

public String getServerInfo ()

public Servlet getServlet (String name)

public String getServletContextName ()

public Enumeration<String> getServletNames ()

public Enumeration<Servlet> getServlets ()

public void log (String message, Throwable ex)

public void log (Exception ex, String message)

public void log (String message)

public void registerContext (String contextPath, ServletContext context)

public void removeAttribute (String name)

public void setAttribute (String name, Object value)

public void setContextPath (String contextPath)

public void setMinorVersion (int minorVersion)

public void setServletContextName (String servletContextName)

Protected Methods

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