Unit Testing & MockK
TESTING › Unit
Writing fast, isolated JVM unit tests with JUnit, MockK, fakes, and dependency injection.
Unit testing is a near-universal Android interview topic because it reveals whether you actually isolate and verify business logic rather than just clicking the app. Interviewers probe your JUnit fluency, MockK API knowledge (especially coroutine and capture support), and your judgment on fakes vs mocks and what is even worth testing. Strong answers tie testability directly to dependency injection and clean architecture.
What this covers
- JUnit basics: @Test, @Before/@After lifecycle, assertions, and rules like InstantTaskExecutorRule or a coroutine test rule
- MockK core API: every {} stubbing, returns/andThen/answers, verify {} with exactly/atLeast/atMost, and verifyOrder vs verifySequence
- Coroutine and capture support: coEvery/coVerify for suspend functions, slot<T>()/capture() to assert passed arguments
- Relaxed mocks, spyk, and annotations (@MockK, @RelaxedMockK, @InjectMockKs) plus when each is appropriate
- Fakes vs mocks: why Google recommends hand-written fakes over mocks for collaborators you own
- What to unit-test (business logic, state transformations, edge cases) vs skip (framework code, trivial getters); structuring code with DI for testability
Study this topic
- Unit Testing & MockK explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- Unit Testing & MockK interview questions and answers