Class AssertableMockMvc
MockMvc
variant that tests Spring MVC exchanges and provides fluent
assertions using AssertJ
.
A main difference with MockMvc
is that an unresolved exception
is not thrown directly. Rather an AssertableMvcResult
is available
with an unresolved
exception
.
AssertableMockMvc
can be configured with a list of
message converters to allow the response
body to be deserialized, rather than asserting on the raw values.
- Since:
- 6.2
- Author:
- Stephane Nicoll, Brian Clozel
-
Method Summary
Modifier and TypeMethodDescriptionstatic AssertableMockMvc
Create aAssertableMockMvc
instance that delegates to the givenMockMvc
instance.static AssertableMockMvc
from
(WebApplicationContext applicationContext) Shortcut to create anAssertableMockMvc
instance using the given, fully initialized (i.e., refreshed)WebApplicationContext
.static AssertableMockMvc
from
(WebApplicationContext applicationContext, Function<DefaultMockMvcBuilder, MockMvc> customizations) Create anAssertableMockMvc
instance using the given, fully initialized (i.e., refreshed)WebApplicationContext
.static AssertableMockMvc
Shortcut to create anAssertableMockMvc
instance by registering one or more@Controller
instances.static AssertableMockMvc
of
(Collection<?> controllers, Function<StandaloneMockMvcBuilder, MockMvc> customizations) Create anAssertableMockMvc
instance by registering one or more@Controller
instances and configuring Spring MVC infrastructure programmatically.perform
(RequestBuilder requestBuilder) Perform a request and return a type that can be used with standardAssertJ
assertions.withHttpMessageConverters
(Iterable<HttpMessageConverter<?>> httpMessageConverters) Return a newAssertableMockMvc
instance using the specified message converters.
-
Method Details
-
create
Create aAssertableMockMvc
instance that delegates to the givenMockMvc
instance.- Parameters:
mockMvc
- the MockMvc instance to delegate calls to
-
from
public static AssertableMockMvc from(WebApplicationContext applicationContext, Function<DefaultMockMvcBuilder, MockMvc> customizations) Create anAssertableMockMvc
instance using the given, fully initialized (i.e., refreshed)WebApplicationContext
. The givencustomizations
are applied to theDefaultMockMvcBuilder
that ultimately creates the underlyingMockMvc
instance.If no further customization of the underlying
MockMvc
instance is required, usefrom(WebApplicationContext)
.- Parameters:
applicationContext
- the application context to detect the Spring MVC infrastructure and application controllers fromcustomizations
- a function that creates aMockMvc
instance based on aDefaultMockMvcBuilder
.- See Also:
-
from
Shortcut to create anAssertableMockMvc
instance using the given, fully initialized (i.e., refreshed)WebApplicationContext
.Consider using
from(WebApplicationContext, Function)
if further customization of the underlyingMockMvc
instance is required.- Parameters:
applicationContext
- the application context to detect the Spring MVC infrastructure and application controllers from- See Also:
-
of
public static AssertableMockMvc of(Collection<?> controllers, Function<StandaloneMockMvcBuilder, MockMvc> customizations) Create anAssertableMockMvc
instance by registering one or more@Controller
instances and configuring Spring MVC infrastructure programmatically.This allows full control over the instantiation and initialization of controllers and their dependencies, similar to plain unit tests while also making it possible to test one controller at a time.
- Parameters:
controllers
- one or more@Controller
instances or@Controller
types to test; a type (Class
) will be turned into an instancecustomizations
- a function that creates aMockMvc
instance based on aStandaloneMockMvcBuilder
, typically to configure the Spring MVC infrastructure- See Also:
-
of
Shortcut to create anAssertableMockMvc
instance by registering one or more@Controller
instances.The minimum infrastructure required by the
DispatcherServlet
to serve requests with annotated controllers is created. Consider usingof(Collection, Function)
if additional configuration of the MVC infrastructure is required.- Parameters:
controllers
- one or more@Controller
instances or@Controller
types to test; a type (Class
) will be turned into an instance- See Also:
-
withHttpMessageConverters
public AssertableMockMvc withHttpMessageConverters(Iterable<HttpMessageConverter<?>> httpMessageConverters) Return a newAssertableMockMvc
instance using the specified message converters.If none are specified, only basic assertions on the response body can be performed. Consider registering a suitable JSON converter for asserting against JSON data structures.
- Parameters:
httpMessageConverters
- the message converters to use- Returns:
- a new instance using the specified converters
-
perform
Perform a request and return a type that can be used with standardAssertJ
assertions.Use static methods of
MockMvcRequestBuilders
to prepare the request, wrapping the invocation inassertThat
. The following asserts that a GET request against "/greet" has an HTTP status code 200 (OK) and a simple body:assertThat(mvc.perform(get("/greet"))) .hasStatusOk() .body().asString().isEqualTo("Hello");
Contrary to
MockMvc.perform(RequestBuilder)
, this does not throw an exception if the request fails with an unresolved exception. Rather, the result provides the exception, if any. Assuming that aPOST
request against/boom
throws anIllegalStateException
, the following asserts that the invocation has indeed failed with the expected error message:assertThat(mvc.perform(post("/boom"))) .unresolvedException().isInstanceOf(IllegalStateException.class) .hasMessage("Expected");
- Parameters:
requestBuilder
- used to prepare the request to execute; see static factory methods inMockMvcRequestBuilders
- Returns:
- an
AssertableMvcResult
to be wrapped inassertThat
- See Also:
-