Flutter: Testing for exceptions in widget tests

To catch exceptions thrown in a flutter test, use WidgetTester.takeException. This returns the last exception caught by the framework.

await tester.tap(find.byIcon(Icons.send));
expect(tester.takeException(), isInstanceOf<UnrecognizedTermException>());

You also don’t need a throwsA matcher, since it is not being thrown from the method.

Leave a Comment