@TestComponent @ComponentScan(basePackageClasses=com.c4_soft.springaddons.test.support.web.SerializationHelper.class) @Scope(value="prototype") public class MockMvcSupport extends Object
Just another wrapper for Spring MockMvc.
It would extend MockMvc if it was not final :-/
com.c4-soft.springaddons.test.web.default-media-type and
com.c4-soft.springaddons.test.web.default-charset to test properties, defaulted to application/json and
utf-8perform and exposes request builder helpers for advanced cases (when you need
to further customize MockHttpServletRequestBuilder with cookies or additional headers for instance)| Modifier and Type | Class and Description |
|---|---|
static class |
MockMvcSupport.MockMvcProperties |
| Constructor and Description |
|---|
MockMvcSupport(org.springframework.test.web.servlet.MockMvc mockMvc,
SerializationHelper serializationHelper,
MockMvcSupport.MockMvcProperties mockMvcProperties) |
| Modifier and Type | Method and Description |
|---|---|
org.springframework.test.web.servlet.ResultActions |
delete(String urlTemplate,
Object... uriVars)
Shortcut to issue a DELETE request (no header)
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
deleteRequestBuilder(String urlTemplate,
Object... uriVars)
Factory for a DELETE request builder.
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
feed(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder request,
T payload,
org.springframework.http.MediaType mediaType,
Charset charset)
Adds serialized payload to request content.
|
org.springframework.test.web.servlet.ResultActions |
get(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Shortcut to issue a GET request with minimal headers and submit it.
|
org.springframework.test.web.servlet.ResultActions |
get(String urlTemplate,
Object... uriVars)
Shortcut to create a builder for a GET request with minimal headers and submit it (Accept header defaulted to what this helper was
constructed with).
|
org.springframework.web.servlet.DispatcherServlet |
getDispatcherServlet() |
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
getRequestBuilder(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Factory providing with a request builder to issue a GET request (with Accept header).
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
getRequestBuilder(String urlTemplate,
Object... uriVars)
Factory providing with a request builder to issue a GET request (with Accept header defaulted to what this helper is constructed with).
|
org.springframework.test.web.servlet.ResultActions |
head(String urlTemplate,
Object... uriVars)
Shortcut to issue a HEAD request (no header)
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
headRequestBuilder(String urlTemplate,
Object... uriVars)
Factory for a HEAD request builder.
|
org.springframework.test.web.servlet.ResultActions |
option(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Shortcut to issue an OPTION request with Accept header
|
org.springframework.test.web.servlet.ResultActions |
option(String urlTemplate,
Object... uriVars)
Shortcut to issue an OPTION request with default media-type as Accept header
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
optionRequestBuilder(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Factory for an OPTION request initialized with an Accept header.
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
optionRequestBuilder(String urlTemplate,
Object... uriVars)
Factory for an OPTION request initialized with an Accept header set to default media-type.
|
<T> org.springframework.test.web.servlet.ResultActions |
patch(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
Shortcut to issue a patch request with Content-type header and a body.
|
<T> org.springframework.test.web.servlet.ResultActions |
patch(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
Shortcut to issue a patch request with Content-type header and a body.
|
<T> org.springframework.test.web.servlet.ResultActions |
patch(T payload,
String urlTemplate,
Object... uriVars)
Shortcut to issue a patch request with Content-type header and a body (using default media-type).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
patchRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
Factory for a patch request builder (with Content-type already set).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
patchRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
Factory for a patch request builder (with Content-type already set).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
patchRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
Factory for a patch request builder (with Content-type set to default media-type).
|
org.springframework.test.web.servlet.ResultActions |
perform(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder)
To be called with fully configured request builder (wraps MockMvc
perform). |
<T> org.springframework.test.web.servlet.ResultActions |
post(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).
|
<T> org.springframework.test.web.servlet.ResultActions |
post(T payload,
org.springframework.http.MediaType contentType,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Shortcut to issue a POST request with provided payload as body, using given media-type for serialization (and Content-type header).
|
<T> org.springframework.test.web.servlet.ResultActions |
post(T payload,
String urlTemplate,
Object... uriVars)
Shortcut to issue a POST request with provided payload as body, using default media-type for serialization (and Content-type header).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
postRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
postRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder containing a body set to payload serialized in given media type (with adequate Content-type header).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
postRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder.
|
<T> org.springframework.test.web.servlet.ResultActions |
put(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
Shortcut to issue a PUT request.
|
<T> org.springframework.test.web.servlet.ResultActions |
put(T payload,
org.springframework.http.MediaType contentType,
String charset,
String urlTemplate,
Object... uriVars)
Shortcut to issue a PUT request.
|
<T> org.springframework.test.web.servlet.ResultActions |
put(T payload,
String urlTemplate,
Object... uriVars)
Shortcut to issue a PUT request (with default media-type as Content-type).
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
putRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder containing a body.
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
putRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder containing a body.
|
<T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
putRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
Factory for a POST request builder containing a body.
|
org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder |
requestBuilder(Optional<org.springframework.http.MediaType> accept,
Optional<Charset> charset,
org.springframework.http.HttpMethod method,
String urlTemplate,
Object... uriVars)
Factory for a generic
MockHttpServletRequestBuilder
with relevant "Accept" and "Content-Type" headers. |
MockMvcSupport |
setCharset(Charset charset) |
MockMvcSupport |
setMediaType(org.springframework.http.MediaType mediaType) |
MockMvcSupport |
setSecure(boolean isSecure) |
MockMvcSupport |
with(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor) |
@Autowired
public MockMvcSupport(org.springframework.test.web.servlet.MockMvc mockMvc,
SerializationHelper serializationHelper,
MockMvcSupport.MockMvcProperties mockMvcProperties)
mockMvc - wrapped Spring MVC testing helperserializationHelper - used to serialize payloads to requested Content-type using Spring registered message convertersmockMvcProperties - default values for media-type, charset and https usagepublic MockMvcSupport setSecure(boolean isSecure)
isSecure - if true, requests are sent with https instead of httppublic MockMvcSupport setMediaType(org.springframework.http.MediaType mediaType)
mediaType - override configured default media-typepublic MockMvcSupport setCharset(Charset charset)
charset - override configured default charsetpublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder(Optional<org.springframework.http.MediaType> accept, Optional<Charset> charset, org.springframework.http.HttpMethod method, String urlTemplate, Object... uriVars)
MockHttpServletRequestBuilder
with relevant "Accept" and "Content-Type" headers. You might prefer to use getRequestBuilder or alike which go further with request pre-configuration or even get,
post(Object, String, Object...) and so on which issue simple requests in one step.accept - should be non-empty when issuing response with body (GET, POST, OPTION), none otherwisecharset - char-set to be used for serialized payloadsmethod - whatever HTTP verb you needurlTemplate - end-point to be requesteduriVars - end-point template placeholders valuespublic org.springframework.test.web.servlet.ResultActions perform(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder requestBuilder)
throws Exception
perform).requestBuilder - fully configured requestException - what
perform throwspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder getRequestBuilder(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
accept - determines request Accept header (and response body format)urlTemplate - API end-point to calluriVars - values to feed URL template placeholderspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder getRequestBuilder(String urlTemplate, Object... uriVars)
urlTemplate - API end-point to calluriVars - values to feed URL template placeholderspublic org.springframework.test.web.servlet.ResultActions get(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
accept - determines request Accept header (and response body format)urlTemplate - API endpoint to be requesteduriVars - values to replace endpoint placeholders withException - what perform throwspublic org.springframework.test.web.servlet.ResultActions get(String urlTemplate, Object... uriVars) throws Exception
urlTemplate - API endpoint to be requesteduriVars - values to replace endpoint placeholders withException - what perform throwspublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - to be serialized as body in contentType formatcontentType - format to be used for payload serializationcharset - char-set for request and responseaccept - how should the response body be serialized (if any)urlTemplate - API end-point to be requesteduriVars - values to replace end-point placeholders withException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - to be serialized as body in contentType formatcontentType - format to be used for payload serializationaccept - how should the response body be serialized (if any)urlTemplate - API end-point to be requesteduriVars - values to replace end-point placeholders withException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder postRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodyurlTemplate - API end-pointuriVars - values ofr URL template placeholdersException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.ResultActions post(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - POST request bodycontentType - media type used to serialize payload and set Content-type headeraccept - media-type to be set as Accept header (and response serialization)charset - char-set for request and responseurlTemplate - API end-point to be calleduriVars - values ofr URL template placeholdersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions post(T payload,
org.springframework.http.MediaType contentType,
org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - POST request bodycontentType - media type used to serialize payload and set Content-type headeraccept - media-type to be set as Accept header (and response serialization)urlTemplate - API end-point to be calleduriVars - values ofr URL template placeholdersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions post(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - POST request bodyurlTemplate - API end-point to be calleduriVars - values ofr URL template placeholdersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - to be serialized as body in contentType formatcontentType - format to be used for payload serializationcharset - char-set for requesturlTemplate - API end-point to be requesteduriVars - values to replace end-point placeholders withException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - to be serialized as body in contentType formatcontentType - format to be used for payload serializationurlTemplate - API end-point to be requesteduriVars - values to replace end-point placeholders withException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder putRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - to be serialized as body in contentType formaturlTemplate - API end-point to be requesteduriVars - values to replace end-point placeholders withException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.ResultActions put(T payload,
org.springframework.http.MediaType contentType,
String charset,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycontentType - payload serialization media-typecharset - char-set for request and responseurlTemplate - API end-point to requesturiVars - values to be used in end-point URL placehodersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions put(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycontentType - payload serialization media-typeurlTemplate - API end-point to requesturiVars - values to be used in end-point URL placehodersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions put(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodyurlTemplate - API end-point to requesturiVars - values to be used in end-point URL placehodersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycharset - char-set to be used for serialized payloadscontentType - payload serialization formaturlTemplate - API end-pointuriVars - values for end-point placeholdersException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycontentType - payload serialization formaturlTemplate - API end-pointuriVars - values for end-point placeholdersException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder patchRequestBuilder(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodyurlTemplate - API end-pointuriVars - values for end-point placeholdersException - if payload serialization goes wrongpublic <T> org.springframework.test.web.servlet.ResultActions patch(T payload,
org.springframework.http.MediaType contentType,
Charset charset,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycontentType - to be used for payload serializationcharset - to be used for payload serializationurlTemplate - end-point URLuriVars - values for end-point URL placeholdersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions patch(T payload,
org.springframework.http.MediaType contentType,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodycontentType - to be used for payload serializationurlTemplate - end-point URLuriVars - values for end-point URL placeholdersException - if payload serialization goes wrong or what
perform throwspublic <T> org.springframework.test.web.servlet.ResultActions patch(T payload,
String urlTemplate,
Object... uriVars)
throws Exception
T - payload typepayload - request bodyurlTemplate - end-point URLuriVars - values for end-point URL placeholdersException - if payload serialization goes wrong or what
perform throwspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder deleteRequestBuilder(String urlTemplate, Object... uriVars)
urlTemplate - API end-pointuriVars - values for end-point URL placeholderspublic org.springframework.test.web.servlet.ResultActions delete(String urlTemplate, Object... uriVars) throws Exception
urlTemplate - API end-pointuriVars - values for end-point URL placeholdersException - what perform throwspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder headRequestBuilder(String urlTemplate, Object... uriVars)
urlTemplate - API end-pointuriVars - values for end-point URL placeholderspublic org.springframework.test.web.servlet.ResultActions head(String urlTemplate, Object... uriVars) throws Exception
urlTemplate - API end-pointuriVars - values for end-point URL placeholdersException - what perform throwspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder optionRequestBuilder(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
accept - response body media-typeurlTemplate - API end-pointuriVars - values for end-point URL placeholderspublic org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder optionRequestBuilder(String urlTemplate, Object... uriVars)
urlTemplate - API end-pointuriVars - values for end-point URL placeholderspublic org.springframework.test.web.servlet.ResultActions option(org.springframework.http.MediaType accept,
String urlTemplate,
Object... uriVars)
throws Exception
accept - response body media-typeurlTemplate - API end-pointuriVars - values for end-point URL placeholdersException - what perform throwspublic org.springframework.test.web.servlet.ResultActions option(String urlTemplate, Object... uriVars) throws Exception
urlTemplate - API end-pointuriVars - values for end-point URL placeholdersException - what perform throwspublic <T> org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder feed(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder request,
T payload,
org.springframework.http.MediaType mediaType,
Charset charset)
throws Exception
MockHttpServletRequestBuilder factories instead
(getRequestBuilder, postRequestBuilder, etc.)T - payload typerequest - builder you want to set body topayload - object to be serialized as bodymediaType - what format you want payload to be serialized to (corresponding HttpMessageConverter must be registered)charset - char-set to be used for payload serializationException - if things go wrong (no registered serializer for payload type and asked MediaType, serialization failure, ...)public org.springframework.web.servlet.DispatcherServlet getDispatcherServlet()
public MockMvcSupport with(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor)
postProcessor - request post-processor to be added to the list of those applied before request is performedMockMvcSupportCopyright © 2021. All rights reserved.