Provides a mechanism to throw exceptions on certain calls. This is useful when working with a Fake and we still want to occasionally make it throw exceptions.
Package selection guidance compiled from public package information, community data, and project usage.
The mock_exceptions package enables developers to simulate exception behavior in mock objects during testing. It allows specific method calls on a fake or mock to throw exceptions, which is particularly useful for testing error-handling scenarios without relying on real implementations. This helps ensure robustness in test cases by validating how code responds to failures. The package integrates seamlessly with Flutter's testing ecosystem and supports all major platforms. It is ideal for unit and integration tests where controlled failure conditions are needed.
Use cases
Testing error handling paths
Simulating failed API responses
Validating retry logic
Ensuring graceful degradation
Testing exception recovery
Mocking inconsistent behavior
Key features
Easy exception injection
Supports all Flutter platforms
Integrates with Fake classes
Minimal setup required
Flexible exception configuration
Works with existing test frameworks
Suitable for
Unit testers
Integration testers
Developers writing resilient code
Teams using TDD/BDD
Those needing realistic failure scenarios
Test-driven development workflows
Considerations
Requires understanding of mocking concepts
Not intended for production use
Depends on proper test structure
May increase test complexity if overused
Best used alongside other testing utilities
Limited to exception simulation only
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
mock_exceptions
Provides a mechanism to throw exceptions on certain calls. This is useful when working with a Fake and we still want to occasionally make it throw exceptions. At a glance:
final f = MyFake();
whenCalling(Invocation.method(#doSomething, null))
.on(f)
.thenThrow(Exception());
expect(() => f.doSomething(), throwsException);
Features
supports mocking exceptions for methods, getters, setters.
supports matching on positional parameters, named parameters and type parameters (generics).
Mockito lets you mock and stub methods. That means it lets you return predefined responses and throw exceptions, but not act as closely to the real thing as a Fake. Since mock_exceptions is supposed to be used on Fakes, its API unambiguously lets you mock only exceptions.
Usage
In your Fake method/getter/setter implementation, add maybeThrowException at the beginning.
In your unit test, set up an expectation, then verify your expectations as usual.
Before
Your Fake method might look like this. It does some real work.
whenCalling(Invocation.method(#doSomething, [equals('fun')])).on(fake).thenThrow(Exception()); is too verbose. Why not reimplement Mockito's API so that I can write when(fake.doSomething('fun')).thenThrow(Exception())?
Implementing Mockito's API requires relying on the noSuchMethod trick to detect the Invocation. As of writing, Mockito's mock.dart takes 1200 lines of code while our mock_exceptions.dart takes around 80. Even if we pare Mockito's down to the minimum (excluding verifications, captures), it'd still take around 500 lines of code.
It also forces the Fakes to extend a pre-defined class, Mock in Mockito's case. In some projects such as Fake Cloud Firestore (example), we actually need to extend another class.
The Mockito API has its own downsides and gotchas. See how one should use named arguments here. Also you need to use its wrappers around the regular Matchers, such as any instead of anything, and argThat(matcher).