ArchitectureMedium30 XP4 min read
What is the testing strategy for a Clean Architecture Flutter app?
TL;DR: Test each layer in isolation: Domain (pure unit tests, no mocks of internals), Data (mock HTTP/DB, test mapping logic), Presentation (bloc_test + golden tests). Integration tests cover critical paths end-to-end.
Full Answer
Test Pyramid
- ▸Unit tests (many): Domain entities, use cases with FakeRepositories, formatters, validators
- ▸Unit tests (many): BLoC/Cubit with mock repositories using bloc_test
- ▸Widget tests (some): Individual widget rendering with mocked BLoCs
- ▸Integration tests (few): Critical flows end-to-end on device/emulator
Layer-by-Layer Strategy
- ▸Domain: No mocking needed — pure Dart functions and FakeRepository (in-memory)
- ▸Data: Mock Dio with DioAdapter/MockClient, test JSON deserialization and error mapping
- ▸BLoC: Use bloc_test's blocTest(), seed initial state, expect state sequence
- ▸Widgets: Use ProviderScope + Riverpod or BlocProvider with mocked blocs
Code Examples
dartUse case test with FakeRepository
Output
// Fast test, no Flutter framework, no network calls // FakeRepository is more reliable than Mockito mocks
Common Mistakes
- ✗Testing the repository implementation with the same tests as the interface — mock at the boundary
- ✗Writing integration tests for everything — they're slow; keep them for critical user journeys
Interview Tip
💡
Show you know the test pyramid. Most tests should be fast unit tests on the domain layer. Widget tests verify rendering. Integration tests are the last resort, not the first.
#testing#unit-test#widget-test#integration-test#test-pyramid