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 MockMvc that sends requests to handler, usually the web starter's dispatcherHandler bean. Create one yourself to add defaultPostProcessors, such as a logged-in user for every request:
MockMvc.of(ApplicationContext context)
The MockMvc of context, sending requests to its dispatcherHandler bean. Throws a StateError when the context has none, that is without spring_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 DELETE to path with headers.
get(String path, {Map<String, String> headers = const {}, Map<String, Object> params = const {}}) → ResultActions
Sends a GET to path with headers and query params; a List value 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-data upload to path: the files and the text fields params as one body, through the same multipart parsing and limits as the server. A POST unless method says otherwise, like Spring's MockMvcRequestBuilders.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 request describes; 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 POST to path. json is encoded as the body with Content-Type: application/json; otherwise body, a String or bytes, is sent as is. When both are given, json wins.
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 path in 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