Class MockServletContext

  • All Implemented Interfaces:
    ServletContext

    public class MockServletContext
    extends Object
    implements ServletContext

    Mock implementation of ServletContext.

    This implementation supports all of the standard context methods except for request dispatching which just indicates what is being dispatched to, rather than performing an actual dispatch.

    The context can be configured with a path parameter that should point to an directory location that represents the place where the contents of the WAR bundle are located. The path can either be an absolute or relative path. If the path is not found, the classpath will be checked for such a directory. Setting this value allows all of the resource location functionality to work as in a fully functioning web application. If this value is not set then not resource location functionality will work and instead null will always be returned.

    This class was adapted from Apache Wicket.

    Since:
    17.10.0
    • Field Detail

      • DEFAULT_CONTEXT_PATH

        public static final String DEFAULT_CONTEXT_PATH
        The servlet context default context path, "/mock".
        See Also:
        Constant Field Values
    • Constructor Detail

      • MockServletContext

        public MockServletContext()
        Default constructor for this mock object. The servlet context name is set to 'mock'. The web content root and temporary work direcotry are set to null.
      • MockServletContext

        public MockServletContext​(String contextPath,
                                  String webappPath)

        Create the mock object. As part of the creation, the context sets the root directory where web application content is stored. This must be an ABSOLUTE directory relative to where the tests are being executed.

        For example: System.getProperty("user.dir") + "/src/webapp"

        In addition to setting the web root directory, this constructor also sets up a temporary work directory for things like file uploads.

        Note this temporary work directory is set as the value of the ServletContext attribute 'javax.servlet.context.tempdir'.

        The temporary work directory defaults to System.getProperty("java.io.tmpdir").

        Parameters:
        contextPath - the servlet context path
        webappPath - The path to the root of the web application
      • MockServletContext

        public MockServletContext​(String contextPath,
                                  String webappPath,
                                  String tempPath)

        Create the mock object. As part of the creation, the context sets the root directory where web application content is stored. This must be an ABSOLUTE directory relative to where the tests are being executed.

        For example: System.getProperty("user.dir") + "/src/webapp"

        In addition to setting the web root directory, this constructor also sets up a temporary work directory for things like file uploads.

        Note this temporary work directory is set as the value of the ServletContext attribute 'javax.servlet.context.tempdir'.

        Parameters:
        contextPath - the servlet context path
        webappPath - the path to the root of the web application
        tempPath - the temporary work directory