
# Testing

In tests, replace the transport with `MockHttpTransport`, which answers with prepared responses and records what was sent. It comes with `spring_boot_starter_test` and plays the part of Spring's `MockRestServiceServer`. The client itself, with its encoding, decoding, error handling and interceptors, runs as in production.

## Why

A test that calls a real remote service is slow, flaky and depends on data you do not control. Replacing the transport keeps everything of the client but the network, so a test checks that the right request goes out and the answer is read correctly.

## Example

```dart
import 'package:spring_boot_starter_test/spring_boot_starter_test.dart';
import 'package:test/test.dart';

import 'spring_gen/main.test.spring.dart';

void main() {
  final server = MockHttpTransport();
  final app = springBootTest(
    springApplication,
    overrides: [BeanOverride<HttpTransport>(server)],
    properties: {'catalog.url': 'https://catalog.test'},
  );

  test('reads a book', () async {
    server.when(HttpMethod.get, '/books/1').respond(200, json: {'isbn': '1', 'title': 'Dune'});
    expect(await app.getBean<Shop>().title('1'), 'Dune');
    expect(server.lastRequest.headers['Accept'], 'application/json');
  });

  test('a missing book is null', () async {
    server.when(HttpMethod.get, '/books/2').respond(404);
    expect(await app.getBean<Shop>().title('2'), isNull);
  });
}
```

`respondEvents([...])` answers a server-sent event stream (see [Server-sent events](server-sent-events.md)).

## Replacing a whole client

To test code that uses a client without HTTP at all, replace the client:

```dart
final app = springBootTest(
  springApplication,
  overrides: [BeanOverride<CatalogClient>(FakeCatalog())],
);

class FakeCatalog implements CatalogClient {
  @override
  Future<Book> find(String isbn) async => Book(isbn: isbn, title: 'Dune');
  // ...
}
```

## The transport

Requests go through the `HttpTransport` bean. By default it is `platformHttpTransport()` of `spring_web_dart`: `dart:io` on the VM and in Flutter (mobile, desktop), `fetch` on the web, where the server must allow your origin with CORS. Declare your own `HttpTransport` bean to send through something else, such as an adapter over another HTTP library. `connect-timeout` (under `default`) configures the default transport.

## How it works

`@FeignClient` is marked `@InterfaceProxy` (from `spring_core_dart`). For such an interface the generator writes a class whose methods hand each call, with the method's annotations, parameter names and types, to an `InvocationHandler` that the starter's `feignClientFactory` bean creates. The client is a regular bean, so AOP applies to it: `@Cacheable` on a client method (with `spring_boot_starter_cache`) caches the responses.

## In Spring (Java)

```java
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
server.expect(requestTo("/books/1")).andRespond(withSuccess(json, MediaType.APPLICATION_JSON));
```

Spring Cloud OpenFeign tests usually use WireMock; `MockHttpTransport` replaces the transport bean instead.

## See also

- [Testing in the web starter](../spring-boot-starter-web/testing.md)
- [spring_web_dart](../spring-web-dart/index.md)
