Robolectric Simulator
Starting with the upcoming Robolectric 4.15 release, it's possible to launch an interactive preview of your app or tests in a Robolectric environment. This has been made possible with the significant enhancements to Robolectric over the past four years, particularly related to native graphics.
Note that the Robolectric simulator is highly experimental and is still largely in the proof-of-concept stage. If you encounter issues, please file bugs on the Robolectric repository. Problems you experience when running the simulator are likely problems in Robolectric itself and can help drive improvements to the testing framework.
The Robolectric simulator can be a useful way to preview how your UI would look in a Robolectric environment. This can be helpful, for instance, when debugging failing Robolectric tests.
Launching the simulator
There are two ways to launch the simulator: with the simulator Gradle plugin or by invoking the simulator APIs from tests.
Using the Gradle plugin
To enable the Robolectric simulator with the Gradle plugin, add the plugin
to the plugins
block of your app's build.gradle
/build.gradle.kts
file:
simulator-gradle-plugin
Then, run ./gradlew simulate
to start the simulator. It will run the main
Activity
that is configured in your manifest.
Standalone API
Inside a Robolectric test, you can also start the simulator using the
standalone API. To do this, first add a dependency to the simulator in your
project's build.gradle
/build.gradle.kts
file.
Simulator dependency
Next, you can create a Simulator object and start it using:
Simulator API
This approach is good of your top-level Activity is unable to be started in a Robolectric environment.