Class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebConnectionBuilderSupport<T>>
java.lang.Object
org.springframework.test.web.servlet.htmlunit.MockMvcWebConnectionBuilderSupport<T>
- Type Parameters:
T
- a self reference to the builder type
- Direct Known Subclasses:
MockMvcHtmlUnitDriverBuilder
,MockMvcWebClientBuilder
public abstract class MockMvcWebConnectionBuilderSupport<T extends MockMvcWebConnectionBuilderSupport<T>>
extends Object
Support class that simplifies the creation of a
WebConnection
that
uses MockMvc
and optionally delegates to a real WebConnection
for specific requests.
The default is to use MockMvc
for requests to localhost
and otherwise use a real WebConnection
.
- Since:
- 4.2
- Author:
- Rob Winch, Sam Brannen
-
Constructor Summary
ModifierConstructorDescriptionprotected
Create a new instance using the suppliedMockMvc
instance.protected
Create a new instance using the suppliedWebApplicationContext
.protected
MockMvcWebConnectionBuilderSupport
(WebApplicationContext context, MockMvcConfigurer configurer) Create a new instance using the suppliedWebApplicationContext
andMockMvcConfigurer
. -
Method Summary
Modifier and TypeMethodDescriptionSpecify thatMockMvc
should always be used regardless of what the request looks like.contextPath
(String contextPath) Set the context path to use.protected final com.gargoylesoftware.htmlunit.WebConnection
createConnection
(com.gargoylesoftware.htmlunit.WebClient webClient) Create a newWebConnection
that will use aMockMvc
instance if one of the specifiedWebRequestMatcher
instances matches.useMockMvc
(WebRequestMatcher... matchers) Add additionalWebRequestMatcher
instances that will ensure thatMockMvc
is used to process the request, if such a matcher matches against the web request.useMockMvcForHosts
(String... hosts) Add additionalWebRequestMatcher
instances that returntrue
if a supplied host matches — for example,"example.com"
or"example.com:8080"
.
-
Constructor Details
-
MockMvcWebConnectionBuilderSupport
Create a new instance using the suppliedMockMvc
instance.- Parameters:
mockMvc
- theMockMvc
instance to use; nevernull
-
MockMvcWebConnectionBuilderSupport
Create a new instance using the suppliedWebApplicationContext
.- Parameters:
context
- theWebApplicationContext
to create aMockMvc
instance from; nevernull
-
MockMvcWebConnectionBuilderSupport
protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context, MockMvcConfigurer configurer) Create a new instance using the suppliedWebApplicationContext
andMockMvcConfigurer
.- Parameters:
context
- theWebApplicationContext
to create aMockMvc
instance from; nevernull
configurer
- the MockMvcConfigurer to apply; nevernull
-
-
Method Details
-
contextPath
Set the context path to use.If the supplied value is
null
or empty, the first path segment of the request URL is assumed to be the context path.Default is
""
.- Parameters:
contextPath
- the context path to use- Returns:
- this builder for further customization
-
alwaysUseMockMvc
Specify thatMockMvc
should always be used regardless of what the request looks like.- Returns:
- this builder for further customization
-
useMockMvc
Add additionalWebRequestMatcher
instances that will ensure thatMockMvc
is used to process the request, if such a matcher matches against the web request.- Parameters:
matchers
- additionalWebRequestMatcher
instances- Returns:
- this builder for further customization
-
useMockMvcForHosts
Add additionalWebRequestMatcher
instances that returntrue
if a supplied host matches — for example,"example.com"
or"example.com:8080"
.- Parameters:
hosts
- additional hosts that ensureMockMvc
gets invoked- Returns:
- this builder for further customization
-
createConnection
protected final com.gargoylesoftware.htmlunit.WebConnection createConnection(com.gargoylesoftware.htmlunit.WebClient webClient) Create a newWebConnection
that will use aMockMvc
instance if one of the specifiedWebRequestMatcher
instances matches.- Parameters:
webClient
- the WebClient to use if none of the specifiedWebRequestMatcher
instances matches (nevernull
)- Returns:
- a new
WebConnection
that will use aMockMvc
instance if one of the specifiedWebRequestMatcher
matches - Since:
- 4.3
- See Also:
-