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
ConstructorsConstructorDescriptionCreate 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 TypeMethodDescriptionvoidaddListener(Class<? extends EventListener> listenerClass) voidaddListener(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.JspConfigDescriptorgetServletRegistration(String servletName) voidInitializes all filters and servlets registered on this context.booleanMethods 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:
addServletin interfacejakarta.servlet.ServletContext- Overrides:
addServletin classorg.springframework.mock.web.MockServletContext
-
addServlet
- Specified by:
addServletin interfacejakarta.servlet.ServletContext- Overrides:
addServletin classorg.springframework.mock.web.MockServletContext
-
addServlet
- Specified by:
addServletin interfacejakarta.servlet.ServletContext- Overrides:
addServletin classorg.springframework.mock.web.MockServletContext
-
getServletRegistration
- Specified by:
getServletRegistrationin interfacejakarta.servlet.ServletContext- Overrides:
getServletRegistrationin classorg.springframework.mock.web.MockServletContext
-
getServletRegistrations
- Specified by:
getServletRegistrationsin interfacejakarta.servlet.ServletContext- Overrides:
getServletRegistrationsin classorg.springframework.mock.web.MockServletContext
-
addFilter
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Overrides:
addFilterin classorg.springframework.mock.web.MockServletContext
-
addFilter
- Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Overrides:
addFilterin classorg.springframework.mock.web.MockServletContext
-
addFilter
public MockFilterRegistration addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass) - Specified by:
addFilterin interfacejakarta.servlet.ServletContext- Overrides:
addFilterin classorg.springframework.mock.web.MockServletContext
-
getFilterRegistration
- Specified by:
getFilterRegistrationin interfacejakarta.servlet.ServletContext- Overrides:
getFilterRegistrationin classorg.springframework.mock.web.MockServletContext
-
getFilterRegistrations
- Specified by:
getFilterRegistrationsin interfacejakarta.servlet.ServletContext- Overrides:
getFilterRegistrationsin classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext- Overrides:
addListenerin classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext- Overrides:
addListenerin classorg.springframework.mock.web.MockServletContext
-
addListener
- Specified by:
addListenerin interfacejakarta.servlet.ServletContext- Overrides:
addListenerin classorg.springframework.mock.web.MockServletContext
-
getJspConfigDescriptor
public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()- Specified by:
getJspConfigDescriptorin interfacejakarta.servlet.ServletContext- Overrides:
getJspConfigDescriptorin 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 correspondingMockServletRegistrationorMockFilterRegistrationthat 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
-