Android Unit Testing Interview Questions
JUnit, mocking, and test design.
50 questions in this topic · 8 sample questions below
Practice Unit Testing in the quiz engine
Sample questions
Where do local unit tests (src/test) run compared to instrumented tests (src/androidTest)?
Why: Local unit tests execute on the host JVM and are fast but lack a real Android framework, while instrumented tests run on an emulator or device where the framework is available. The claim that both run the same way ignores this fundamental split.
Why does calling an Android framework method like Log.d directly from a plain local unit test throw an exception by default?
Why: The android.jar used for local unit tests contains stubbed methods that throw a Method not mocked RuntimeException unless returnDefaultValues or Robolectric is used. It is not about permissions, which have no bearing on host-JVM stubs.
In the classic taxonomy of test doubles, what distinguishes a fake from a stub?
Why: A fake has real, working behavior taken as a shortcut (e.g. an in-memory repository), whereas a stub merely returns pre-programmed responses to calls made during the test. Treating them as interchangeable erases the behavioral-vs-canned distinction that guides which to pick.
What primarily distinguishes a mock from a spy?
Why: A spy delegates to the real object except where you override behavior, while a mock is a synthetic double whose interactions you typically verify. Saying a spy cannot verify is wrong: spies support the same verification API.
A test creates a mockk of the exact class it is trying to test and stubs the method under test. What is the core problem?
Why: Mocking the class under test replaces the real logic with canned answers, so the assertions merely confirm the stub you wrote. MockK actually can mock final classes by default, so the compile claim is false.
Why are fakes often preferred over mocks for a repository dependency in ViewModel tests?
Why: A fake repository behaves like the real thing (storing and returning data), so tests survive internal refactors, whereas over-specified mocks break when call order or internal methods change. Fakes usually require more code to write, not less.
What does a relaxed mock in MockK provide that a strict mock does not?
Why: A relaxed mock returns defaults (0, empty string, empty collections, a relaxed child mock) for any call you did not explicitly stub, avoiding MissingMethodInvocation errors. It does not turn the object into a spy nor disable verify.
Which MockK construct is required to stub a suspend function?
Why: Suspend functions must be stubbed with coEvery and verified with coVerify because these open a coroutine-capable stubbing scope. Plain every cannot call a suspend function in its lambda.
Practice all 50 Unit Testing questions
These 8 are a sample. The full Unit Testing bank is scored, tracks your progress, and explains every answer.
More Android interview topics
- Android Hilt interview questions
- Android Coroutines & Flow interview questions
- Android Room interview questions
- Android Design Patterns interview questions
- Android Mobile System Design interview questions
- Android Coding Interview Patterns interview questions
- Android NDK interview questions
- Android Sensors interview questions
- Android Security interview questions
- Android Jetpack Compose interview questions
- Android Canvas & Animation interview questions
- Android CI/CD interview questions
- Android Git interview questions
- Android Kotlin interview questions
- Android Retrofit interview questions
- Android Architecture interview questions
- Android Android Framework interview questions
- Android Kotlin Multiplatform interview questions
- Android WorkManager & Background interview questions
- Android Performance & Memory interview questions