0Pricing
Flutter Mobile Development · Lesson

Unit Testing & Mocking

Write fast, isolated unit tests for your Flutter business logic and use mocks to test code that depends on services and APIs.

Unit Testing & Mocking is a free Flutter Mobile Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flutter Mobile Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Testing Pyramid

You have seen widget and integration tests. At the base sits the fastest layer: unit tests that verify a single function or class in isolation.

  • Many unit tests (fast)
  • Fewer widget tests
  • Even fewer integration tests (slow)

Anatomy of a Unit Test

A unit test uses test() and expect() from the test package.

import 'package:test/test.dart';

void main() {
  test('adds two numbers', () {
    expect(2 + 3, equals(5));
  });
}

Testing a Class

Instantiate the class under test and assert on its behaviour.

test('counter increments', () {
  final counter = Counter();
  counter.increment();
  expect(counter.value, 1);
});

Matchers

Matchers express expectations clearly.

  • equals(x)
  • isTrue / isNull
  • throwsException
  • contains(x)
expect(list, contains('apple'));
expect(() => parse('bad'), throwsFormatException);

Grouping Tests

Use group() to organize related tests and share setup.

group('Cart', () {
  test('starts empty', () { ... });
  test('adds items', () { ... });
});

setUp & tearDown

setUp runs before each test; tearDown runs after — perfect for fresh fixtures.

late Cart cart;
setUp(() => cart = Cart());
tearDown(() => cart.clear());

The Problem of Dependencies

What if your class calls a real API or database? Tests would be slow and flaky. The solution is mocking — replacing dependencies with fakes you control.

Mockito Setup

Add mockito and build_runner, then annotate to generate mocks.

@GenerateMocks([ApiService])
void main() {}
// flutter pub run build_runner build

Stubbing Methods

Use when(...).thenReturn(...) to define what a mock returns.

final mockApi = MockApiService();
when(mockApi.fetchUser()).thenAnswer((_) async => User('Ada'));

Verifying Interactions

verify confirms a method was called the expected number of times.

await repository.loadUser();
verify(mockApi.fetchUser()).called(1);

Testing Async Code

Mark the test body async and await futures, or use the completes matcher.

test('loads data', () async {
  final result = await repo.load();
  expect(result, isNotEmpty);
});

Quick Check

Why would you use a mock in a unit test?

Recap

You learned unit testing and mocking:

  • test/expect/matchers and group/setUp
  • Mockito to generate mocks
  • when().thenReturn and verify().called
  • Testing async code

Unit tests give you fast confidence in your business logic.

Frequently asked questions

Is the “Unit Testing & Mocking” lesson free?

Yes — the full text of “Unit Testing & Mocking” is free to read here on the web, and the Flutter Mobile Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flutter Mobile Development course, upgrade to CoddyKit PRO.

What will I learn in “Unit Testing & Mocking”?

Write fast, isolated unit tests for your Flutter business logic and use mocks to test code that depends on services and APIs. You practise Flutter Mobile Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flutter Mobile Development?

No prior experience is required. Flutter Mobile Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Unit Testing & Mocking” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flutter Mobile Development lesson?

Yes. Every Flutter Mobile Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Widget Testing Principles
  2. Integration Testing
  3. Debugging Tools & Techniques
  4. Unit Testing & Mocking
← Back to Flutter Mobile Development