using-robolectric-correctly
CommunityRun Android-aware JVM tests safely with Robolectric.
Software Engineering#robolectric#androidjunit4#jvm unit testing#includeandroidresources#shadowlooper#agp sharedtest#android-host-test
Authorskydoves
Version1.0.0
Installs0
System Documentation
What problem does it solve?
Robolectric unit tests often fail or behave inconsistently because of runner misconfiguration, missing Android resource setup, incorrect SDK/qualifier targeting, and forgetting to drain the paused main looper before assertions.
Core Features & Use Cases
- Runner choice & configuration discipline: Select AndroidJUnit4 versus RobolectricTestRunner based on whether the test uses Robolectric-specific APIs, and lock behavior with the correct @Config(minSdk / sdk / qualifiers / application).
- Android resources that actually load on the JVM: Ensure unit tests can access R.*, Resources, layouts, and themes by enabling includeAndroidResources.
- Looper draining to make callbacks deterministic: Properly advance/drain paused Handler/Looper work using ShadowLooper and shadowOf(Looper.getMainLooper()).idle() before asserting outcomes.
- Shadows that map to real Android behavior gaps: Use common shadows (ShadowApplication, ShadowLog, ShadowLooper, ShadowPackageManager, ShadowSystemClock) and shadowOf(...) to validate system interactions.
- Modern sharedTest migration for AGP 7.2+: Avoid broken legacy sharedTest srcDir sharing and instead use androidHostTest + androidDeviceTest (or a shared module/KMP setup) for running the same logical tests on both runtimes.
Quick Start
Ask an AI to configure Robolectric for a JVM unit test suite by using AndroidJUnit4 with the right @Config matrix, enabling includeAndroidResources, and adding looper draining via ShadowLooper before assertions.
Dependency Matrix
Required Modules
None requiredComponents
Standard package💻 Claude Code Installation
Recommended: Let Claude install automatically. Simply copy and paste the text below to Claude Code.
Please help me install this Skill: Name: using-robolectric-correctly Download link: https://github.com/skydoves/android-testing-skills/archive/main.zip#using-robolectric-correctly Please download this .zip file, extract it, and install it in the .claude/skills/ directory.
Agent Skills Search Helper
Install a tiny helper to your Agent, search and equip skill from 471,000+ vetted skills library on demand.