Package com.helger.web.scope.mock
Class WebScopeTestRule
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- com.helger.web.scope.mock.WebScopeTestRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
@NotThreadSafe public class WebScopeTestRule extends org.junit.rules.ExternalResourceJUnit test rule for unit tests requiring web scopes.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static StringMOCK_CONTEXT_PATHMock servlet context name
-
Constructor Summary
Constructors Constructor Description WebScopeTestRule()WebScopeTestRule(com.helger.commons.collection.impl.ICommonsMap<String,String> aServletContextInitParameters)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafter()voidbefore()protected com.helger.servlet.mock.MockHttpServletRequestcreateMockRequest(com.helger.servlet.mock.MockServletContext aServletContext)Create a new mock requestprotected com.helger.servlet.mock.MockServletContextcreateMockServletContext(String sContextPath, Map<String,String> aInitParams)Create a new mock servlet contextStringgetContextPath()com.helger.servlet.mock.MockHttpServletRequestgetRequest()com.helger.servlet.mock.MockServletContextgetServletContext()com.helger.commons.collection.impl.ICommonsMap<String,String>getServletContextInitParameters()com.helger.servlet.mock.MockServletPoolgetServletPool()jakarta.servlet.http.HttpSessiongetSession(boolean bCreateIfNotExisting)protected voidinitListener()This method triggers the initialization of theMockHttpListener.WebScopeTestRulesetContextPath(String sContextPath)WebScopeTestRulesetServletContextInitParameters(com.helger.commons.collection.impl.ICommonsMap<String,String> aServletContextInitParameters)StringtoString()
-
-
-
Field Detail
-
MOCK_CONTEXT_PATH
public static final String MOCK_CONTEXT_PATH
Mock servlet context name- See Also:
- Constant Field Values
-
-
Method Detail
-
setContextPath
@Nonnull public final WebScopeTestRule setContextPath(@Nullable String sContextPath)
-
setServletContextInitParameters
@Nonnull public final WebScopeTestRule setServletContextInitParameters(@Nullable com.helger.commons.collection.impl.ICommonsMap<String,String> aServletContextInitParameters)
-
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 theMockHttpListener. 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 benull.aInitParams- The initialization context parameters to use. May benull.- 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. Nevernull.- Returns:
- May be
nullto indicate that the request is added manually - this is helpful for servlet testing.
-
before
@OverrideOnDemand @OverridingMethodsMustInvokeSuper public void before()
- Overrides:
beforein classorg.junit.rules.ExternalResource
-
after
@OverrideOnDemand @OverridingMethodsMustInvokeSuper public void after()
- Overrides:
afterin classorg.junit.rules.ExternalResource
-
getServletContext
@Nullable public final com.helger.servlet.mock.MockServletContext getServletContext()
- Returns:
- The created
MockServletContextornullif non has been created yet.
-
getServletPool
@Nullable public final com.helger.servlet.mock.MockServletPool getServletPool()
- Returns:
- The
MockServletPoolof theMockServletContextornullif no servlet context has been created yet.
-
getRequest
@Nullable public final com.helger.servlet.mock.MockHttpServletRequest getRequest()
- Returns:
- The created
MockHttpServletRequestornullif non has been created yet.
-
getSession
@Nullable public final jakarta.servlet.http.HttpSession getSession(boolean bCreateIfNotExisting)
- Parameters:
bCreateIfNotExisting-trueto create a new session, if non is existing yet. This has only an effect if a request is present.- Returns:
- The
HttpSessionornullif no session was created or if no request is present.
-
-