Skip to content
tezvyn:

Robolectric: Run Android Unit Tests on the JVM

Source: robolectric.orgMediumHow cards are made

Robolectric: Run Android Unit Tests on the JVM

Robolectric runs Android unit tests on a regular JVM, not a slow emulator. Use it in CI/TDD to test logic like Activity lifecycles without the minutes-long build-deploy cycle. The footgun is over-mocking; Robolectric tests behavior, not just implementation.

Why it exists

Running tests on an Android emulator or physical device is slow. The cycle of building, dexing, packaging, deploying, and running can take minutes, which makes rapid iteration and practices like Test-Driven Development (TDD) nearly impossible.

The mental model

Think of Robolectric as a high-speed simulator for the Android framework that runs directly inside a standard Java Virtual Machine (JVM). Instead of interacting with a full-blown emulator, your tests run against a sandboxed, in-memory version of the Android OS, giving you speed and control.

How it works

Robolectric executes your tests on your workstation or CI server's JVM, completely bypassing the need for an emulator. It achieves this by providing special test doubles, called "shadows," for Android SDK classes. These shadows intercept calls to the Android framework and simulate their behavior. For example, Robolectric handles View inflation, resource loading, and system service interactions that would normally require native C code on a device. Each test runs in an isolated sandbox, allowing for precise configuration and preventing tests from interfering with each other.

When to use it

Use Robolectric for unit tests that need to interact with the Android framework but don't require the full fidelity of a physical device. It's perfect for CI pipelines and local development where you want to run tests quickly, such as when practicing TDD. It excels at testing things like Activity lifecycles, UI interactions like button clicks, and resource-dependent logic.

When not to use it

An alternate approach is to use mocking frameworks like Mockito to mock out the entire Android SDK. This often leads to brittle tests that are reverse implementations of your application code. Robolectric allows for a style closer to black-box testing, focusing on the behavior of the application. This makes your tests more effective for refactoring. You can still use a mocking framework with Robolectric, but you don't have to mock Android itself.

One canonical example

A common use case is testing an Activity's response to a user action. A test can programmatically create an Activity using Robolectric's APIs, simulate a button click via performClick(), and then assert that a TextView's text has changed as expected. This entire sequence runs in milliseconds on the JVM, without ever launching an emulator or installing an APK.

Interview question

What is the main benefit of using Robolectric for Android unit tests?

  • a.It removes the dependency on the Android SDK for test compilation.
  • b.It allows direct execution of native C/C++ code within JVM tests.
  • c.It mandates mocking all Android framework components to ensure test isolation.
  • d.It enables fast execution of tests interacting with the Android framework on a standard JVM.Correct
Why?

Robolectric's core purpose is to enable rapid execution of Android framework-dependent unit tests directly on a standard JVM, bypassing the slow build-deploy cycle of emulators. While mocking frameworks can be used, Robolectric specifically aims to reduce the need to mock the entire Android SDK by providing 'shadows' that simulate its behavior.

Just read this? Test yourself on what you have been reading.

Read the original → robolectric.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles