Android Development Android Testing 1 — Questions and Answers
Question 1: Which testing framework is used for writing unit tests in Android projects?
- JUnit (Correct answer)
- Espresso
- Robolectric
- Mockito
Correct answer: JUnit
JUnit is the standard testing framework for writing and running unit tests in Android and Java/Kotlin projects.
Question 2: What is the purpose of Espresso in Android testing?
- To write UI instrumentation tests that interact with the app UI (Correct answer)
- To mock network requests
- To test database operations
- To unit test ViewModels
Correct answer: To write UI instrumentation tests that interact with the app UI
Espresso is Google's UI testing framework that allows you to write automated UI tests that interact with and assert the state of your app's UI.
Question 3: What is a unit test in Android development?
- A test that validates a single function or class in isolation without Android framework dependencies (Correct answer)
- A test that runs on a physical device
- A test that covers the entire app flow
- A test that validates network calls
Correct answer: A test that validates a single function or class in isolation without Android framework dependencies
Unit tests validate individual units of code like functions or classes in isolation, typically running on the JVM without Android dependencies.
Question 4: What is the difference between unit tests and instrumentation tests in Android?
- Unit tests run on JVM; instrumentation tests run on an Android device or emulator (Correct answer)
- Unit tests are slower than instrumentation tests
- Instrumentation tests don't require an emulator
- There is no difference
Correct answer: Unit tests run on JVM; instrumentation tests run on an Android device or emulator
Unit tests run locally on the JVM and are fast, while instrumentation tests run on an Android device/emulator and can access the Android framework.
Question 5: Which library is used for mocking dependencies in Android unit tests?
- Mockito (Correct answer)
- Espresso
- Robolectric
- JUnit
Correct answer: Mockito
Mockito is a popular mocking framework that creates fake implementations of dependencies, allowing you to isolate the code under test.
Question 6: Where are Android unit tests located in a project structure?
- src/test/java (Correct answer)
- src/androidTest/java
- src/main/test
- test/unit/java
Correct answer: src/test/java
Unit tests are placed in the src/test/java directory and run locally on the JVM without Android framework dependencies.
Which testing framework is used for writing unit tests in Android projects?