0Pricing
Android Academy · Lesson

Mocking with Mockito

Isolate dependencies with Mockito and MockK. Stub return values, verify interactions, capture arguments, and use spies for partial mocking.

What Is Mocking?

A mock is a fake object that replaces a real dependency in tests. Unlike a Fake (a working in-memory implementation), a mock:

  • Is generated automatically by a library
  • Records which methods were called and with what arguments
  • Returns whatever you tell it to return (stubbing)
  • Lets you verify interactions after the test

Mockito Setup

Add Mockito and MockK (Kotlin-first alternative) to app/build.gradle:

// app/build.gradle:
testImplementation 'org.mockito:mockito-core:5.10.0'
testImplementation 'org.mockito.kotlin:mockito-kotlin:5.2.1'

// MockK (Kotlin-idiomatic alternative to Mockito):
testImplementation 'io.mockk:mockk:1.13.10'

All lessons in this course

  1. Unit Testing with JUnit
  2. Mocking with Mockito
  3. UI Testing with Espresso
  4. Debugging & Profiling
← Back to Android Academy