MockMvc class final
Sends HTTP requests to the application's controllers in memory, without a server or a socket, and checks the responses.
Use it to test the web layer as a client sees it: URL mapping, request
binding, @Valid checks, filters, exception handlers and the JSON or
ProblemDetail answers. It runs the same dispatcher a real server
runs, so a test that passes here behaves the same over HTTP, but it is
faster and needs no free port. With spring_boot_starter_web,
app.mockMvc of a springBootTest (or an @Autowired() late MockMvc mvc; field in a test class) gives one.
Each call returns a ResultActions: chain ResultActions.andExpect
to check the response, and await the chain, or a failed expectation
is never reported.
final app = springBootTest(springApplication);
test('finds a book, rejects an invalid one', () async {
final mvc = app.mockMvc;
await mvc.get('/api/books/978-0').andExpect(
status: 200,
json: {'isbn': '978-0', 'title': 'Dune'},
);
// An empty ISBN fails validation: a 400 with a ProblemDetail body.
await mvc
.post('/api/books', json: {'isbn': ''})
.andExpect(
status: 400,
contentType: MediaType.applicationProblemJson,
);
});
Spring (Java):
mockMvc.perform(get("/api/books/978-0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("Dune"));
Constructors
-
MockMvc(HttpHandler handler, {List<
RequestPostProcessor> defaultPostProcessors = const []}) -
A
MockMvcthat sends requests tohandler, usually the web starter'sdispatcherHandlerbean. Create one yourself to adddefaultPostProcessors, such as a logged-in user for every request: - MockMvc.of(ApplicationContext context)
-
The
MockMvcofcontext, sending requests to itsdispatcherHandlerbean. Throws a StateError when the context has none, that is withoutspring_boot_starter_web.factory
Properties
-
defaultPostProcessors
→ List<
RequestPostProcessor> -
Applied to every request, before the post-processors of the request
itself: a default user, common headers.
final
- handler → HttpHandler
-
The handler that answers the requests, as the server would call it.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
delete(
String path, {Map< String, String> headers = const {}}) → ResultActions -
Sends a
DELETEtopathwithheaders. -
get(
String path, {Map< String, String> headers = const {}, Map<String, Object> params = const {}}) → ResultActions -
Sends a
GETtopathwithheadersand queryparams; aListvalue sends the parameter several times ({'id': [1, 2]}). -
multipart(
String path, {List< MultipartFile> files = const [], Map<String, String> params = const {}, Map<String, String> headers = const {}, HttpMethod method = HttpMethod.post}) → ResultActions -
Sends a
multipart/form-dataupload topath: thefilesand the text fieldsparamsas one body, through the same multipart parsing and limits as the server. APOSTunlessmethodsays otherwise, like Spring'sMockMvcRequestBuilders.multipart. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
patch(
String path, {Object? json, Object? body, Map< String, String> headers = const {}}) → ResultActions -
Sends a
PATCH, with a body as for post. -
perform(
MockRequestBuilder request) → ResultActions -
Sends the request that
requestdescribes; the shortcuts get, post and the others build one for you. Use it for what they do not cover: cookies, a principal, a post-processor, another method. -
post(
String path, {Object? json, Object? body, Map< String, String> headers = const {}}) → ResultActions -
Sends a
POSTtopath.jsonis encoded as the body withContent-Type: application/json; otherwisebody, aStringor bytes, is sent as is. When both are given,jsonwins. -
put(
String path, {Object? json, Object? body, Map< String, String> headers = const {}}) → ResultActions -
Sends a
PUT, with a body as for post. -
toString(
) → String -
A string representation of this object.
inherited
-
webSocket(
String path, {Map< String, String> headers = const {}, List<RequestPostProcessor> postProcessors = const []}) → Future<WebSocketSession> -
Opens a WebSocket connection to the endpoint at
pathin memory and returns the client's end of it; the application's handler gets the server's end.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited