testing library

Test doubles for the web: fake HTTP and WebSocket connections, and in-memory server requests and responses.

Import it in tests only. It holds what a test needs to run web code without a network:

  • MockHttpTransport replaces the HTTP connection of a client (a Feign client): it answers with prepared responses, event streams or failures, and records what was sent;
  • MockWebSocketTransport replaces the WebSocket connection of a client, and the test plays the server;
  • MockServerHttpRequest, MockServerHttpResponse and MockMultipartFile are the request, response and upload that MockMvc (in spring_boot_starter_test) passes to the application;
  • RequestPostProcessor changes a MockMvc request before it is sent (a logged-in user, a CSRF token).
import 'package:spring_web_dart/testing.dart';

// The remote catalog service answers without a network.
final transport = MockHttpTransport()
  ..when(HttpMethod.get, '/books/1').respond(200, json: {'isbn': '1'});

Spring (Java): MockRestServiceServer, and the mock request and response of spring-test (MockHttpServletRequest, MockHttpServletResponse, MockMultipartFile).

Classes

FunctionRequestPostProcessor
A RequestPostProcessor made from a function, for a one-off change that does not need a class.
MockHttpTransport
A fake HTTP connection for tests: it answers requests with responses prepared in advance, without any network, and records every request sent.
MockMultipartFile
An uploaded file made in a test, with its content in memory.
MockResponseBuilder
Sets how a MockHttpTransport answers one prepared request; returned by MockHttpTransport.when.
MockServerHttpRequest
An HTTP request built in memory, to call the application in a test without a server.
MockServerHttpResponse
An HTTP response kept in memory, so a test can read what the application answered: status, headers, cookies and body.
MockWebSocket
One connection opened through a MockWebSocketTransport: the client uses it as its WebSocketSession, and the test plays the server with the other members.
MockWebSocketTransport
A fake WebSocket connection for tests: every connection the client opens is kept in memory, and the test plays the server on it.
RequestPostProcessor
A step that changes a test request just before MockMvc sends it to the application.
WebSocketSessionPair
Two WebSocketSessions connected to each other in memory: what one sends, the other receives, and closing one closes both.