Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsJul 27, 2026·3 min de lecture

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.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Robolectric
Commande d'installation directe
npx -y tokrepo@latest install caba7ba9-89bf-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires