类 MockServletContext

  • 所有已实现的接口:
    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).

    从以下版本开始:
    1.0.2
    作者:
    Rod Johnson, Juergen Hoeller, Sam Brannen
    另请参阅:
    MockServletContext(org.springframework.core.io.ResourceLoader), AnnotationConfigWebApplicationContext, XmlWebApplicationContext, GenericWebApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext