Class MockAcrossServletContext
- All Implemented Interfaces:
jakarta.servlet.ServletContext
MockServletContext
that also keeps track of all registration actions that
have occurred on servlets, filters and listeners. The MockAcrossServletContext
can be queried
to verify registration was done correctly.
This implementation can act as a not-initialized ServletContext
that allows for dynamic registration
of filters and servlets. It can be used in an integration test scenario to have any
ServletContextInitializer
bean in the
AcrossContext
to execute its configuration.
The latter is also the default behaviour when creating a new MockAcrossServletContext
. Once the context
has been initialized by calling initialize()
, all registration operations will throw an
IllegalStateException
. Initializing the context will call the corresponding init()
methods
on the filters and servlets registered.
- Author:
- Marc Vanbrabant, Arne Vandamme
- See Also:
-
Field Summary
Fields inherited from interface jakarta.servlet.ServletContext
ORDERED_LIBS, TEMPDIR
-
Constructor Summary
ConstructorDescriptionCreate a new instance that acts as if it has not yet been initialized.MockAcrossServletContext
(boolean dynamicRegistrationAllowed) Create a new instance with the specified initialized status. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Class<? extends EventListener> listenerClass) void
addListener
(String className) <T extends EventListener>
voidaddListener
(T t) addServlet
(String servletName, jakarta.servlet.Servlet servlet) addServlet
(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) addServlet
(String servletName, String className) getFilterRegistration
(String filterName) jakarta.servlet.descriptor.JspConfigDescriptor
getServletRegistration
(String servletName) void
Initializes all filters and servlets registered on this context.boolean
Methods inherited from class org.springframework.mock.web.MockServletContext
addInitParameter, addJspFile, addMimeType, createFilter, createListener, createServlet, declareRoles, getAttribute, getAttributeNames, getClassLoader, getContext, getContextPath, getDeclaredRoles, getDefaultServletName, getDefaultSessionTrackingModes, getEffectiveMajorVersion, getEffectiveMinorVersion, getEffectiveSessionTrackingModes, getInitParameter, getInitParameterNames, getMajorVersion, getMimeType, getMinorVersion, getNamedDispatcher, getRealPath, getRequestCharacterEncoding, getRequestDispatcher, getResource, getResourceAsStream, getResourceLocation, getResourcePaths, getResponseCharacterEncoding, getServerInfo, getServletContextName, getSessionCookieConfig, getSessionTimeout, getVirtualServerName, log, log, registerContext, registerNamedDispatcher, removeAttribute, setAttribute, setContextPath, setDefaultServletName, setEffectiveMajorVersion, setEffectiveMinorVersion, setInitParameter, setMajorVersion, setMinorVersion, setRequestCharacterEncoding, setResponseCharacterEncoding, setServletContextName, setSessionTimeout, setSessionTrackingModes, unregisterNamedDispatcher
-
Constructor Details
-
MockAcrossServletContext
public MockAcrossServletContext()Create a new instance that acts as if it has not yet been initialized. -
MockAcrossServletContext
public MockAcrossServletContext(boolean dynamicRegistrationAllowed) Create a new instance with the specified initialized status. If dynamic registration is allowed, the instance will not yet be initialized and will require a call toinitialize()
.- Parameters:
dynamicRegistrationAllowed
- true if the context should not yet be initialized
-
-
Method Details
-
addServlet
public MockServletRegistration addServlet(String servletName, Class<? extends jakarta.servlet.Servlet> servletClass) - Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
- Overrides:
addServlet
in classorg.springframework.mock.web.MockServletContext
-
addServlet
- Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
- Overrides:
addServlet
in classorg.springframework.mock.web.MockServletContext
-
addServlet
- Specified by:
addServlet
in interfacejakarta.servlet.ServletContext
- Overrides:
addServlet
in classorg.springframework.mock.web.MockServletContext
-
getServletRegistration
- Specified by:
getServletRegistration
in interfacejakarta.servlet.ServletContext
- Overrides:
getServletRegistration
in classorg.springframework.mock.web.MockServletContext
-
getServletRegistrations
- Specified by:
getServletRegistrations
in interfacejakarta.servlet.ServletContext
- Overrides:
getServletRegistrations
in classorg.springframework.mock.web.MockServletContext
-
addFilter
- Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
- Overrides:
addFilter
in classorg.springframework.mock.web.MockServletContext
-
addFilter
- Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
- Overrides:
addFilter
in classorg.springframework.mock.web.MockServletContext
-
addFilter
public MockFilterRegistration addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass) - Specified by:
addFilter
in interfacejakarta.servlet.ServletContext
- Overrides:
addFilter
in classorg.springframework.mock.web.MockServletContext
-
getFilterRegistration
- Specified by:
getFilterRegistration
in interfacejakarta.servlet.ServletContext
- Overrides:
getFilterRegistration
in classorg.springframework.mock.web.MockServletContext
-
getFilterRegistrations
- Specified by:
getFilterRegistrations
in interfacejakarta.servlet.ServletContext
- Overrides:
getFilterRegistrations
in classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
- Overrides:
addListener
in classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
- Overrides:
addListener
in classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListener
in interfacejakarta.servlet.ServletContext
- Overrides:
addListener
in classorg.springframework.mock.web.MockServletContext
-
getJspConfigDescriptor
public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()- Specified by:
getJspConfigDescriptor
in interfacejakarta.servlet.ServletContext
- Overrides:
getJspConfigDescriptor
in classorg.springframework.mock.web.MockServletContext
-
getListeners
- Returns:
- list of all listeners that were registered in their raw type (class name, class or instance)
-
initialize
public void initialize()Initializes all filters and servlets registered on this context. This will call eitherServlet.init(ServletConfig)
orFilter.init(FilterConfig)
with the correspondingMockServletRegistration
orMockFilterRegistration
that is registered.Unfortunately since the actual config will be a dummy implementation, it is possible that some classes will not support it. In most cases this should not be an issue however. Initialization will however only occur on registered instances, if a filter or servlet was registered by type or class name only, it will be skipped.
Can be called safely multiple times, actual initialization will occur only once. Once this method has been called, all registration methods will throw an exception.
-
isInitialized
public boolean isInitialized()- Returns:
- true if context acts as initialized and
initialize()
has been called
-