Testing

springBootTest from spring_boot_starter_test works under flutter_test. It starts the application once for the file's tests, with test properties and fakes. You put the widget under a SpringScope with the test's context, and under a SpringApp for the app widget, themes and texts of the properties. Beans are replaced with BeanOverride.

Why

The widgets get their state and services from the application context, not from their constructors. So a test starts the application (or the part it needs), replaces some beans with fakes, and shows the widget with that context. The same test tools serve a Spring server's tests, so a team learns them once.

Setup

# pubspec.yaml
dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0
  spring_boot_starter_test: ^0.9.0

A widget test

The counter app of the overview is a Material app, so the test imports Material too:

import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:my_app/main.dart';
import 'package:my_app/spring_gen/main.spring.dart';
import 'package:spring_boot_starter_flutter_material/spring_boot_starter_flutter_material.dart';
import 'package:spring_boot_starter_test/spring_boot_starter_test.dart';

void main() {
  // Starts the application once for this file, with a property and a fake.
  final app = springBootTest(
    springApplication,
    properties: {'counter.step': 2},
    overrides: [BeanOverride<Clock>(FixedClock())],
  );

  setUp(app.markDirty); // a fresh context, so a fresh Counter, for every test

  testWidgets('a tap adds counter.step', (tester) async {
    await tester.pumpWidget(
      SpringScope(
        applicationContext: app.context,
        child: const SpringApp(home: CounterPage()),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    await tester.tap(find.byIcon(Icons.add));
    await tester.pump();

    expect(find.text('2'), findsOneWidget);
    expect(app.context.getBean<Counter>().value, 2);
  });
}

Themes in tests

A property selects the theme shown:

final app = springBootTest(
  springApplication,
  properties: {'spring.flutter.theme.active': 'dark'},
);

Or the test selects one, as a settings page would:

app.context.getBean<ThemeSelection>().select(AppThemes.ocean);
await tester.pumpAndSettle();

With active: system, the theme follows tester.platformDispatcher.platformBrightnessTestValue: set it to Brightness.dark to test the device's dark mode. The theme's fonts are loaded from the assets in tests as in the app, so golden files show them instead of the test font.

Platforms

springBootTest adds no platform profile. A bean or a file of a platform profile is tested with that profile in the test's properties ('spring.profiles.active': 'ios') and Flutter's debugDefaultTargetPlatformOverride for the widgets.

The whole start

To test the start itself (the splash, a failed start, the platform profiles), pump SpringApplicationWidget as runApp does:

testWidgets('the app starts', (tester) async {
  await tester.pumpWidget(
    SpringApplicationWidget(
      application: () => springApplication,
      home: const CounterPage(),
    ),
  );
  await tester.pumpAndSettle();

  // The test runs as Android, in a debug build: those profiles are active.
  final context = SpringScope.of(tester.element(find.byType(CounterPage)));
  expect(context.environment.activeProfiles, ['mobile', 'android', 'debug']);

  await tester.pumpWidget(const SizedBox()); // removing it closes the context
});

SpringApplicationWidget takes no test properties or bean overrides. A test that needs them passes its own SpringApplication, built from a copy of springApplication with the overrides added.

Widgets with fakes, without an application

A SpringStatelessWidget can be built with fakes and no application at all:

final widget = const BasketBadge().buildWith(context, (FakeBasket(), FakeClock()));

Files and directories

A widget test has no path_provider plugin. FileTemplate then has no device directories: replace its store with BeanOverride<FileStore>(InMemoryFileStore()). SQLite databases stay where their URL says (see SQLite databases).

Golden files

matchesGoldenFile works as in any Flutter test. Pump the screen under a SpringApp, select each theme in turn, and compare one golden per theme; the theme examples of the framework do so for every theme they ship.

See also