
# Testing

A widget test of the navigation starts the app as `flutter run` does, with `SpringApplicationWidget`, and navigates through the `RouteTemplate` bean. The routes, shells, filters and guards are the real ones.

## Why

Routes are checked at the start, so the first test of the navigation is simply that the app starts. After that, a test drives the router as the user would (a tap) or as a link would (`go('/address')`), and checks which screen shows.

## Example

```dart
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/main.dart';
import 'package:my_app/spring_gen/main.spring.dart';
import 'package:spring_boot_starter_flutter_navigation/spring_boot_starter_flutter_navigation.dart';

void main() {
  testWidgets('an unknown address shows the not-found screen', (tester) async {
    await tester.pumpWidget(
      SpringApplicationWidget(application: () => springApplication),
    );
    await tester.pumpAndSettle();

    final context = SpringScope.of(tester.element(find.byType(CatalogPage)));
    await context.getBean<RouteTemplate>().go('/nope');
    await tester.pumpAndSettle();

    expect(find.byType(NotFoundPage), findsOneWidget);
  });

  testWidgets('a book opens over the catalog, and back returns', (tester) async {
    await tester.pumpWidget(
      SpringApplicationWidget(application: () => springApplication),
    );
    await tester.pumpAndSettle();

    final routes = SpringScope.of(tester.element(find.byType(CatalogPage)))
        .getBean<RouteTemplate>();
    await routes.go(BookPageRoute.to(isbn: '978-0441013593'));
    await tester.pumpAndSettle();
    expect(find.byType(BookPage), findsOneWidget);

    routes.pop();
    await tester.pumpAndSettle();
    expect(find.byType(CatalogPage), findsOneWidget);
  });
}
```

- The `RouteTemplate` bean reads and drives the screen on top; `state`, `uri` and `stack` say where the app is.
- A test that needs fakes or test properties passes its own `SpringApplication`, built from a copy of `springApplication` with the overrides added (`SpringApplicationWidget` takes none itself).
- In a widget test the platform is Android, so deep links of the platform are not involved; `go` plays the part of a link.

## See also

- [Testing in spring_boot_starter_flutter](../spring-boot-starter-flutter/testing.md)
- [Screens and routes](screens.md)
