spring-framework / org.springframework.mock.web / MockServletContext

MockServletContext

open class MockServletContext : ServletContext

Mock implementation of the javax.servlet.ServletContext interface.

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

Compatible with Servlet 3.1 but can be configured to expose a specific version through #setMajorVersion/#setMinorVersion; 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.

Author
Rod Johnson

Author
Juergen Hoeller

Author
Sam Brannen

Since
1.0.2

See Also
#MockServletContext(org.springframework.core.io.ResourceLoader)org.springframework.web.context.support.AnnotationConfigWebApplicationContextorg.springframework.web.context.support.XmlWebApplicationContextorg.springframework.web.context.support.GenericWebApplicationContext

Constructors

<init>

MockServletContext()

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

MockServletContext(resourceBasePath: String)

Create a new MockServletContext, using a DefaultResourceLoader.

MockServletContext(resourceLoader: ResourceLoader)

Create a new MockServletContext, using the specified ResourceLoader and no base path.

MockServletContext(resourceBasePath: String, resourceLoader: ResourceLoader)

Create a new MockServletContext using the supplied resource base path and resource loader.

Registers a MockRequestDispatcher for the Servlet named 'default'.

Functions

addFilter

open fun addFilter(filterName: String, className: String): Dynamic
open fun addFilter(filterName: String, filter: Filter): Dynamic
open fun addFilter(filterName: String, filterClass: Class<out Filter>): Dynamic

addInitParameter

open fun addInitParameter(name: String, value: String): Unit

addJspFile

open fun addJspFile(servletName: String, jspFile: String): Dynamic

addListener

open fun addListener(listenerClass: Class<out EventListener>): Unit
open fun addListener(className: String): Unit
open fun <T : EventListener> addListener(t: T): Unit

addMimeType

open fun addMimeType(fileExtension: String, mimeType: MediaType): Unit

Adds a mime type mapping for use by #getMimeType(String).

addServlet

open fun addServlet(servletName: String, className: String): Dynamic
open fun addServlet(servletName: String, servlet: Servlet): Dynamic
open fun addServlet(servletName: String, servletClass: Class<out Servlet>): Dynamic

createFilter

open fun <T : Filter> createFilter(c: Class<T>): T

createListener

open fun <T : EventListener> createListener(c: Class<T>): T

createServlet

open fun <T : Servlet> createServlet(c: Class<T>): T

declareRoles

open fun declareRoles(vararg roleNames: String): Unit

getAttribute

open fun getAttribute(name: String): Any

getAttributeNames

open fun getAttributeNames(): Enumeration<String>

getClassLoader

open fun getClassLoader(): ClassLoader

getContext

open fun getContext(contextPath: String): ServletContext

getContextPath

open fun getContextPath(): String

getDeclaredRoles

open fun getDeclaredRoles(): MutableSet<String>

getDefaultServletName

open fun getDefaultServletName(): String

Get the name of the default Servlet.

Defaults to 'default'.

getDefaultSessionTrackingModes

open fun getDefaultSessionTrackingModes(): MutableSet<SessionTrackingMode>

getEffectiveMajorVersion

open fun getEffectiveMajorVersion(): Int

getEffectiveMinorVersion

open fun getEffectiveMinorVersion(): Int

getEffectiveSessionTrackingModes

open fun getEffectiveSessionTrackingModes(): MutableSet<SessionTrackingMode>

getFilterRegistration

open fun getFilterRegistration(filterName: String): FilterRegistration

This method always returns null.

getFilterRegistrations

open fun getFilterRegistrations(): MutableMap<String, out FilterRegistration>

This method always returns an empty map.

getInitParameter

open fun getInitParameter(name: String): String

getInitParameterNames

open fun getInitParameterNames(): Enumeration<String>

getJspConfigDescriptor

open fun getJspConfigDescriptor(): JspConfigDescriptor

getMajorVersion

open fun getMajorVersion(): Int

getMimeType

open fun getMimeType(filePath: String): String

getMinorVersion

open fun getMinorVersion(): Int

getNamedDispatcher

open fun getNamedDispatcher(path: String): RequestDispatcher

getRealPath

open fun getRealPath(path: String): String

getRequestCharacterEncoding

open fun getRequestCharacterEncoding(): String

getRequestDispatcher

open fun getRequestDispatcher(path: String): RequestDispatcher

getResource

open fun getResource(path: String): URL

getResourceAsStream

open fun getResourceAsStream(path: String): InputStream

getResourcePaths

open fun getResourcePaths(path: String): MutableSet<String>

getResponseCharacterEncoding

open fun getResponseCharacterEncoding(): String

getServerInfo

open fun getServerInfo(): String

getServlet

open fun getServlet(name: String): Servlet

getServletContextName

open fun getServletContextName(): String

getServletNames

open fun getServletNames(): Enumeration<String>

getServletRegistration

open fun getServletRegistration(servletName: String): ServletRegistration

This method always returns null.

getServletRegistrations

open fun getServletRegistrations(): MutableMap<String, out ServletRegistration>

This method always returns an empty map.

getServlets

open fun getServlets(): Enumeration<Servlet>

getSessionCookieConfig

open fun getSessionCookieConfig(): SessionCookieConfig

getSessionTimeout

open fun getSessionTimeout(): Int

getVirtualServerName

open fun getVirtualServerName(): String

log

open fun log(message: String): Unit
open fun log(ex: Exception, message: String): Unit
open fun log(message: String, ex: Throwable): Unit

registerContext

open fun registerContext(contextPath: String, context: ServletContext): Unit

registerNamedDispatcher

open fun registerNamedDispatcher(name: String, requestDispatcher: RequestDispatcher): Unit

Register a RequestDispatcher (typically a MockRequestDispatcher) that acts as a wrapper for the named Servlet.

removeAttribute

open fun removeAttribute(name: String): Unit

setAttribute

open fun setAttribute(name: String, value: Any): Unit

setContextPath

open fun setContextPath(contextPath: String): Unit

setDefaultServletName

open fun setDefaultServletName(defaultServletName: String): Unit

Set the name of the default Servlet.

Also unregisters the current default RequestDispatcher and replaces it with a MockRequestDispatcher for the provided defaultServletName.

setEffectiveMajorVersion

open fun setEffectiveMajorVersion(effectiveMajorVersion: Int): Unit

setEffectiveMinorVersion

open fun setEffectiveMinorVersion(effectiveMinorVersion: Int): Unit

setInitParameter

open fun setInitParameter(name: String, value: String): Boolean

setMajorVersion

open fun setMajorVersion(majorVersion: Int): Unit

setMinorVersion

open fun setMinorVersion(minorVersion: Int): Unit

setRequestCharacterEncoding

open fun setRequestCharacterEncoding(requestCharacterEncoding: String): Unit

setResponseCharacterEncoding

open fun setResponseCharacterEncoding(responseCharacterEncoding: String): Unit

setServletContextName

open fun setServletContextName(servletContextName: String): Unit

setSessionTimeout

open fun setSessionTimeout(sessionTimeout: Int): Unit

setSessionTrackingModes

open fun setSessionTrackingModes(sessionTrackingModes: MutableSet<SessionTrackingMode>): Unit

unregisterNamedDispatcher

open fun unregisterNamedDispatcher(name: String): Unit

Unregister the RequestDispatcher with the given name.