Class MockAcrossServletContext

java.lang.Object
org.springframework.mock.web.MockServletContext
com.foreach.across.test.MockAcrossServletContext
All Implemented Interfaces:
jakarta.servlet.ServletContext

public class MockAcrossServletContext extends org.springframework.mock.web.MockServletContext
Extension of the Spring 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:
  • 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 to initialize().
      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 interface jakarta.servlet.ServletContext
      Overrides:
      addServlet in class org.springframework.mock.web.MockServletContext
    • addServlet

      public MockServletRegistration addServlet(String servletName, String className)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
      Overrides:
      addServlet in class org.springframework.mock.web.MockServletContext
    • addServlet

      public MockServletRegistration addServlet(String servletName, jakarta.servlet.Servlet servlet)
      Specified by:
      addServlet in interface jakarta.servlet.ServletContext
      Overrides:
      addServlet in class org.springframework.mock.web.MockServletContext
    • getServletRegistration

      public MockServletRegistration getServletRegistration(String servletName)
      Specified by:
      getServletRegistration in interface jakarta.servlet.ServletContext
      Overrides:
      getServletRegistration in class org.springframework.mock.web.MockServletContext
    • getServletRegistrations

      public Map<String,MockServletRegistration> getServletRegistrations()
      Specified by:
      getServletRegistrations in interface jakarta.servlet.ServletContext
      Overrides:
      getServletRegistrations in class org.springframework.mock.web.MockServletContext
    • addFilter

      public MockFilterRegistration addFilter(String filterName, String className)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
      Overrides:
      addFilter in class org.springframework.mock.web.MockServletContext
    • addFilter

      public MockFilterRegistration addFilter(String filterName, jakarta.servlet.Filter filter)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
      Overrides:
      addFilter in class org.springframework.mock.web.MockServletContext
    • addFilter

      public MockFilterRegistration addFilter(String filterName, Class<? extends jakarta.servlet.Filter> filterClass)
      Specified by:
      addFilter in interface jakarta.servlet.ServletContext
      Overrides:
      addFilter in class org.springframework.mock.web.MockServletContext
    • getFilterRegistration

      public MockFilterRegistration getFilterRegistration(String filterName)
      Specified by:
      getFilterRegistration in interface jakarta.servlet.ServletContext
      Overrides:
      getFilterRegistration in class org.springframework.mock.web.MockServletContext
    • getFilterRegistrations

      public Map<String,MockFilterRegistration> getFilterRegistrations()
      Specified by:
      getFilterRegistrations in interface jakarta.servlet.ServletContext
      Overrides:
      getFilterRegistrations in class org.springframework.mock.web.MockServletContext
    • addListener

      public void addListener(Class<? extends EventListener> listenerClass)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
      Overrides:
      addListener in class org.springframework.mock.web.MockServletContext
    • addListener

      public void addListener(String className)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
      Overrides:
      addListener in class org.springframework.mock.web.MockServletContext
    • addListener

      public <T extends EventListener> void addListener(T t)
      Specified by:
      addListener in interface jakarta.servlet.ServletContext
      Overrides:
      addListener in class org.springframework.mock.web.MockServletContext
    • getJspConfigDescriptor

      public jakarta.servlet.descriptor.JspConfigDescriptor getJspConfigDescriptor()
      Specified by:
      getJspConfigDescriptor in interface jakarta.servlet.ServletContext
      Overrides:
      getJspConfigDescriptor in class org.springframework.mock.web.MockServletContext
    • getListeners

      public List<Object> 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 either Servlet.init(ServletConfig) or Filter.init(FilterConfig) with the corresponding MockServletRegistration or MockFilterRegistration 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