Class MockMvcTester
MockMvcTester provides support for testing Spring MVC applications
with MockMvc for server request handling using
AssertJ.
A tester instance can be created from a WebApplicationContext:
// Create an instance with default settings
MockMvcTester mvc = MockMvcTester.from(applicationContext);
// Create an instance with a custom Filter
MockMvcTester mvc = MockMvcTester.from(applicationContext,
builder -> builder.addFilters(filter).build());
A tester can be created in standalone mode by providing the controller(s) to include:
// Create an instance for PersonController
MockMvcTester mvc = MockMvcTester.of(new PersonController());
Once a tester instance is available, you can perform requests in a similar
fashion as with MockMvc, and wrapping the result in
assertThat() provides access to assertions. For instance:
// perform a GET on /hi and assert the response body is equal to Hello
assertThat(mvc.perform(get("/hi")))
.hasStatusOk().hasBodyTextEqualTo("Hello");
One main difference between MockMvc and MockMvcTester is
that an unresolved exception is not thrown directly when using
MockMvcTester. Rather an MvcTestResult is available with an
unresolved exception
which allows you to assert that a request failed unexpectedly:
// perform a GET on /boom and assert the message for the the unresolved exception
assertThat(mvc.perform(get("/boom")))
.hasUnresolvedException())
.withMessage("Test exception");
MockMvcTester 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 MockMvcTesterCreate aMockMvcTesterinstance that delegates to the givenMockMvcinstance.static MockMvcTesterfrom(WebApplicationContext applicationContext) Shortcut to create anMockMvcTesterinstance using the given, fully initialized (i.e., refreshed)WebApplicationContext.static MockMvcTesterfrom(WebApplicationContext applicationContext, Function<DefaultMockMvcBuilder, MockMvc> customizations) Create anMockMvcTesterinstance using the given, fully initialized (i.e., refreshed)WebApplicationContext.static MockMvcTesterShortcut to create anMockMvcTesterinstance by registering one or more@Controllerinstances.static MockMvcTesterof(Collection<?> controllers, Function<StandaloneMockMvcBuilder, MockMvc> customizations) Create anMockMvcTesterinstance by registering one or more@Controllerinstances and configuring Spring MVC infrastructure programmatically.perform(RequestBuilder requestBuilder) Perform a request and return aresultthat can be used with standardAssertJassertions.withHttpMessageConverters(Iterable<HttpMessageConverter<?>> httpMessageConverters) Return a newMockMvcTesterinstance using the specified message converters.
-
Method Details
-
create
Create aMockMvcTesterinstance that delegates to the givenMockMvcinstance.- Parameters:
mockMvc- the MockMvc instance to delegate calls to
-
from
public static MockMvcTester from(WebApplicationContext applicationContext, Function<DefaultMockMvcBuilder, MockMvc> customizations) Create anMockMvcTesterinstance using the given, fully initialized (i.e., refreshed)WebApplicationContext. The givencustomizationsare applied to theDefaultMockMvcBuilderthat ultimately creates the underlyingMockMvcinstance.If no further customization of the underlying
MockMvcinstance is required, usefrom(WebApplicationContext).- Parameters:
applicationContext- the application context to detect the Spring MVC infrastructure and application controllers fromcustomizations- a function that creates aMockMvcinstance based on aDefaultMockMvcBuilder- See Also:
-
from
Shortcut to create anMockMvcTesterinstance using the given, fully initialized (i.e., refreshed)WebApplicationContext.Consider using
from(WebApplicationContext, Function)if further customization of the underlyingMockMvcinstance is required.- Parameters:
applicationContext- the application context to detect the Spring MVC infrastructure and application controllers from- See Also:
-
of
public static MockMvcTester of(Collection<?> controllers, Function<StandaloneMockMvcBuilder, MockMvc> customizations) Create anMockMvcTesterinstance by registering one or more@Controllerinstances 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@Controllerinstances or@Controllertypes to test; a type (Class) will be turned into an instancecustomizations- a function that creates aMockMvcinstance based on aStandaloneMockMvcBuilder, typically to configure the Spring MVC infrastructure- See Also:
-
of
Shortcut to create anMockMvcTesterinstance by registering one or more@Controllerinstances.The minimum infrastructure required by the
DispatcherServletto 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@Controllerinstances or@Controllertypes to test; a type (Class) will be turned into an instance- See Also:
-
withHttpMessageConverters
public MockMvcTester withHttpMessageConverters(Iterable<HttpMessageConverter<?>> httpMessageConverters) Return a newMockMvcTesterinstance 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 aresultthat can be used with standardAssertJassertions.Use static methods of
MockMvcRequestBuildersto 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 aPOSTrequest against/boomthrows 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
MvcTestResultto be wrapped inassertThat - See Also:
-