Class MockServletContext

All Implemented Interfaces:

public class MockServletContext extends Object implements ServletContext
Mock implementation of the 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.

Rod Johnson, Juergen Hoeller, Sam Brannen
See Also: