0Pricing
Jetpack Compose Academy · Lesson

Testing ViewModels & Flows

Unit-test logic with test dispatchers.

Testing ViewModels & Flows is a free Jetpack Compose Academy 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Test Logic Without the UI

A ViewModel holds your screen logic, so you can test it directly with plain JUnit. No emulator or rendering needed, which makes these tests fast. ⚡

Arrange, Act, Assert

ViewModel tests follow a simple rhythm: create the ViewModel, call a function, then assert the new state. Keep each test focused on one behavior.

val vm = CounterViewModel()
vm.increment()
assertEquals(1, vm.count)

The Coroutine Challenge

Many ViewModels launch coroutines, which run on Dispatchers.Main. In tests there is no main looper, so you must swap in a test dispatcher instead.

StandardTestDispatcher

A StandardTestDispatcher gives you full control over coroutine timing. Tasks queue up and run only when you advance the virtual clock.

val dispatcher = StandardTestDispatcher()

Replace the Main Dispatcher

Use Dispatchers.setMain before each test to inject your test dispatcher, then call resetMain afterward to leave global state clean.

Dispatchers.setMain(dispatcher)

runTest

Wrap suspending test bodies in runTest. It provides a coroutine scope with a virtual clock so delays resolve instantly, not in real seconds.

@Test fun loads() = runTest {
    // suspend calls run here
}

Quick Check

Your ViewModel launches coroutines on the main dispatcher. What lets a unit test control their execution?

Advancing the Clock

With a StandardTestDispatcher, queued coroutines wait. Call advanceUntilIdle inside runTest to run them all, then check the final state.

advanceUntilIdle()
assertEquals(Loaded, vm.uiState.value)

Testing a StateFlow

ViewModels often expose a StateFlow. Its current snapshot lives in .value, so you can read and assert it directly after advancing the clock.

assertEquals(0, vm.count.value)

Collecting Flow Emissions

To capture a sequence of emissions, the Turbine library makes Flow testing easy with awaitItem, asserting each value as it arrives.

vm.events.test {
    assertEquals(Saved, awaitItem())
}

Fake Your Dependencies

Inject a fake repository that returns canned data. This keeps tests fast and lets you simulate success and error paths without a real network.

val vm = NewsViewModel(FakeRepo())

Clean Teardown

Always pair setMain with resetMain in your teardown. Leaving a test dispatcher installed can leak into and break other tests in the suite.

@After fun tearDown() {
    Dispatchers.resetMain()
}

Recap: ViewModels & Flows

Test logic without UI by swapping in a test dispatcher, running code in runTest, and asserting StateFlow values. Fast, focused, reliable tests. 🏁

Frequently asked questions

Is the “Testing ViewModels & Flows” lesson free?

Yes — the full text of “Testing ViewModels & Flows” is free to read here on the web, and the Jetpack Compose Academy 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Testing ViewModels & Flows”?

Unit-test logic with test dispatchers. You practise Jetpack Compose Academy 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 Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy 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 “Testing ViewModels & Flows” 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 Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy 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. createComposeRule & Finders
  2. Assertions & Performing Actions
  3. Semantics & Test Tags
  4. Testing ViewModels & Flows
← Back to Jetpack Compose Academy