Robolectric: Configuration Changes, CI & When to Use Real Devices
Simulating Configuration Changes
Robolectric provides APIs to programmatically simulate a rotation or other configuration change within a JVM test -- letting a team verify an Activity correctly preserves state across recreation, a common real source of Android bugs, without physically rotating an actual device.
Fragment Testing
A Fragment's behavior is tied to its host Activity's lifecycle and the framework's Fragment management machinery. FragmentScenario-based Robolectric tests can launch a Fragment within a simulated hosting Activity and walk it through lifecycle events as a fast JVM test.
Speed: Local Development & CI
No emulator boot time, no APK deployment step. A developer can run relevant tests directly from the IDE for near-instant feedback while iterating. In CI, this avoids the cost of provisioning emulator infrastructure for every single run -- a genuine operational advantage for a large test suite run on every commit.
The Limits of Simulation
Shadow classes approximate real Android framework behavior -- they aren't guaranteed to match every single edge case perfectly. Many teams keep a smaller set of genuine instrumented tests for behavior particularly sensitive to real device/OS specifics (hardware sensors, subtle rendering), while relying on Robolectric for the bulk of everyday testing.
Incremental Migration
A large existing codebase with a slow instrumented test suite doesn't need a risky, all-at-once rewrite -- start by converting the slowest, most frequently-run tests that don't actually need device-specific behavior, gradually shrinking the instrumented suite over time.
Freeing Code From Testability-Driven Constraints
Without Robolectric, a team might over-architect code specifically to avoid Android framework dependencies just to keep tests fast. Robolectric relieves that pressure -- code can interact naturally with Context, Views, and SDK classes where genuinely appropriate, while tests stay fast.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free