Class WebScopeTestRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    @NotThreadSafe
    public class WebScopeTestRule
    extends org.junit.rules.ExternalResource
    JUnit test rule for unit tests requiring web scopes.
    Author:
    Philip Helger
    • Constructor Detail

      • WebScopeTestRule

        public WebScopeTestRule()
      • WebScopeTestRule

        public WebScopeTestRule​(@Nullable
                                com.helger.commons.collection.impl.ICommonsMap<String,​String> aServletContextInitParameters)
    • Method Detail

      • getServletContextInitParameters

        @Nonnull
        @ReturnsMutableCopy
        public final com.helger.commons.collection.impl.ICommonsMap<String,​String> getServletContextInitParameters()
      • initListener

        @OverrideOnDemand
        protected void initListener()
        This method triggers the initialization of the MockHttpListener. It is called before the main servlet context is created.
      • createMockServletContext

        @Nonnull
        @OverrideOnDemand
        protected com.helger.servlet.mock.MockServletContext createMockServletContext​(@Nullable
                                                                                      String sContextPath,
                                                                                      @Nullable
                                                                                      Map<String,​String> aInitParams)
        Create a new mock servlet context
        Parameters:
        sContextPath - The context path to use. May be null.
        aInitParams - The initialization context parameters to use. May be null.
        Returns:
        Never null.
      • createMockRequest

        @Nullable
        @OverrideOnDemand
        protected com.helger.servlet.mock.MockHttpServletRequest createMockRequest​(@Nonnull
                                                                                   com.helger.servlet.mock.MockServletContext aServletContext)
        Create a new mock request
        Parameters:
        aServletContext - The servlet context to use. Never null.
        Returns:
        May be null to indicate that the request is added manually - this is helpful for servlet testing.
      • getServletContext

        @Nullable
        public final com.helger.servlet.mock.MockServletContext getServletContext()
        Returns:
        The created MockServletContext or null if non has been created yet.
      • getServletPool

        @Nullable
        public final com.helger.servlet.mock.MockServletPool getServletPool()
        Returns:
        The MockServletPool of the MockServletContext or null if no servlet context has been created yet.
      • getRequest

        @Nullable
        public final com.helger.servlet.mock.MockHttpServletRequest getRequest()
        Returns:
        The created MockHttpServletRequest or null if non has been created yet.
      • getSession

        @Nullable
        public final jakarta.servlet.http.HttpSession getSession​(boolean bCreateIfNotExisting)
        Parameters:
        bCreateIfNotExisting - true to create a new session, if non is existing yet. This has only an effect if a request is present.
        Returns:
        The HttpSession or null if no session was created or if no request is present.