
# Testing

A test has no GPU: a `Scene` cannot be made there. In a widget test of the app the engine fails to start, logged once as a warning, and the scene widgets are their placeholders. The rest of the app is tested as always.

## Why

The app's code has no branch for a device that cannot draw scenes, so a test needs none either: it sees the app as such a device does. What is left to choose is whether the test hears the warning, and whether the beans around the engine (the models, the events, the status) run.

## 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_scene/spring_boot_starter_flutter_scene.dart';
import 'package:spring_boot_starter_test/spring_boot_starter_test.dart';

void main() {
  // No engine is started: the scene widgets are their placeholders.
  final app = springBootTest(
    springApplication,
    properties: {'spring.flutter.scene.render': 'off'},
  );

  testWidgets('the showroom shows its placeholder', (tester) async {
    await tester.pumpWidget(
      SpringScope(
        applicationContext: app.context,
        child: const SpringApp(home: Showroom()),
      ),
    );

    expect(find.text('hero'), findsOneWidget);
  });
}
```

## The three ways

| The test | What happens |
|---|---|
| changes nothing | When a scene first shows, the engine fails to start, logged once as a warning; `SceneFailedEvent` and `SceneUnavailableEvent` are published; the widgets are their placeholders |
| sets `spring.flutter.scene.render=off` | The engine is never started and nothing is logged; the widgets are their placeholders |
| replaces the `SceneBackend` bean | The engine starts without a GPU, or fails when the test says so: the beans around it (the events, the status, the models) are tested |

`spring.flutter.scene.enabled=false` in the test's properties tests the app with no scene bean at all.

## Beans a test replaces

`SceneBackend` is what draws the scenes, started once by the `SceneEngine`. `SceneModelImporter` turns a model's file into its node tree. The starter's own beans stand back for an app's or a test's:

```dart
import 'dart:typed_data';

import 'package:flutter_scene/scene.dart' show Node;
import 'package:spring_boot_starter_flutter_scene/spring_boot_starter_flutter_scene.dart';

/// An engine without a GPU: starts, or fails when told to.
final class FakeBackend implements SceneBackend {
  FakeBackend({this.fails = false});

  final bool fails;

  @override
  String get name => 'fake';

  @override
  Future<void> start() async {
    if (fails) {
      throw StateError('no GPU here');
    }
  }
}

/// Makes a bare node for any file.
final class FakeImporter implements SceneModelImporter {
  @override
  Future<Node> import(Uint8List bytes, {required String name}) async =>
      Node(name: name);
}
```

```dart
final app = springBootTest(
  springApplication,
  overrides: [
    BeanOverride<SceneBackend>(FakeBackend()),
    BeanOverride<SceneModelImporter>(FakeImporter()),
  ],
);

test('the engine starts', () async {
  final engine = app.context.getBean<SceneEngine>();
  expect(await engine.start(), isTrue);
  expect(engine.state, SceneEngineState.ready);
});
```

With a backend that starts, a `SpringSceneView.declarative` builds the engine's view, and a scene that draws needs a GPU. So a backend of the test is for tests of beans. A test of widgets sets `render=off`, or gives a backend that fails.

## See also

- [Testing in spring_boot_starter_flutter](../spring-boot-starter-flutter/testing.md)
- [Where scenes do not draw](without-scenes.md)
- [spring_boot_starter_test](../spring-boot-starter-test/index.md)
