# Robolectric — Fast Android Unit Testing Without an Emulator > Industry-standard framework for running Android unit tests in a simulated environment inside the JVM, delivering 10x faster test execution than emulator-based approaches. ## Install Save in your project root: # Robolectric — Fast Android Unit Testing Without an Emulator ## Quick Use ```groovy // build.gradle testImplementation "org.robolectric:robolectric:4.14" ``` ```java @RunWith(AndroidJUnit4.class) public class MyActivityTest { @Test public void clickButton_showsText() { ActivityScenario scenario = ActivityScenario.launch(MyActivity.class); scenario.onActivity(activity -> { activity.findViewById(R.id.button).performClick(); assertEquals("Hello", ((TextView) activity.findViewById(R.id.text)).getText()); }); } } ``` ## Introduction Robolectric is the standard framework for Android unit testing. It simulates the Android runtime inside a regular JVM process, so tests run on your workstation in seconds rather than minutes on an emulator. Google recommends Robolectric for local unit tests in the official Android developer documentation. ## What Robolectric Does - Simulates Android framework classes (Activity, Service, ContentProvider) on the JVM - Supports 14 Android API levels from API 23 through API 36 - Provides shadow classes that replace Android internals with testable implementations - Integrates with AndroidX Test APIs (ActivityScenario, Espresso matchers) - Runs as a standard JUnit test, compatible with Gradle and Maven ## Architecture Overview Robolectric intercepts calls to the Android SDK at the bytecode level using a custom class loader. When your test creates an Activity, Robolectric's shadow classes handle lifecycle management, view inflation, and resource loading without starting a real Android runtime. Shadows are pluggable: the framework ships defaults, and you can write custom shadows for proprietary SDKs. ## Self-Hosting & Configuration - Add `testImplementation "org.robolectric:robolectric:4.14"` to your Gradle file - Annotate test classes with `@RunWith(AndroidJUnit4.class)` - Set the target SDK in `robolectric.properties` or per-test annotations - Run tests with `./gradlew test` just like standard JUnit tests - Configure shadow packages for third-party libraries in the properties file ## Key Features - 10x faster than emulator-based instrumentation tests - Full Android lifecycle simulation (create, start, resume, pause, destroy) - Resource and asset loading from your project's res/ directory - Compatible with AndroidX Test, Mockito, and Truth assertions - No flakiness from emulator boot failures or network timeouts ## Comparison with Similar Tools - **Android Emulator (Instrumentation Tests)** — runs on a real Android image; Robolectric runs on the JVM for speed - **Espresso** — UI testing on-device; Robolectric handles unit-level tests locally - **Mockito** — general mocking library; Robolectric provides Android-specific simulation - **MockK** — Kotlin mocking; complements Robolectric for Kotlin-based Android tests - **Paparazzi** — screenshot testing without a device; Robolectric tests behavior, not visuals ## FAQ **Q: Can Robolectric replace all instrumentation tests?** A: No. Use Robolectric for fast unit tests and Espresso for integration and UI acceptance tests that need a real rendering pipeline. **Q: Does it work with Jetpack Compose?** A: Partial support exists. Robolectric can host Compose tests, though some Compose-specific APIs may require a real device. **Q: How does Robolectric stay current with new Android versions?** A: Google engineers contribute to Robolectric. New API level shadows are typically available within weeks of an Android release. **Q: Is it compatible with Kotlin?** A: Yes. Write tests in Kotlin or Java; Robolectric works with both. ## Sources - https://github.com/robolectric/robolectric - https://robolectric.org --- Source: https://tokrepo.com/en/workflows/asset-caba7ba9 Author: AI Open Source