Fork me on GitHub

Robolectric

Who’s Using Robolectric?

Presentation: TDD Android Applications with Robolectric

Pivotal Labs developers have given this presentation several times.


Troubleshooting

java.lang.RuntimeException: Stub!


Could not resolve dependencies for project: Could not find artifact com.google.android.maps:maps:jar:16_r3 in central (http://repo1.maven.org/maven2)

The jerk lawyers at Google won’t allow the Google maps add-on library stubs to be uploaded to Maven Central. You need to manually install them yourself.

Make sure you’ve got Android SDK 16 or later downloaded, then do this:

cd $ANDROID_HOME
ls -1d add-ons/addon-google_apis-google-* | sort | tail -1 |
    xargs -I% mvn install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=16_r3 -Dpackaging=jar -Dfile=%/libs/maps.jar

Unable to find Android SDK

NOTE: this problem no longer happens in Robolectric 2.0. You oughta upgrade!

Robolectric cannot find your Android SDK. You can tell Robolectric how to find your SDK root in several ways:

local.properties file

Set the sdk.dir in a local.properties file by running the following in your project’s root dir:

$ android update project -p .

Setting up a local.properties file is a solution that will work for most IDEs since you don’t need to worry about getting environment variables passed around.

ANDROID_HOME environment variable

Set ANDROID_HOME environment variable. You can put this in your .bash_profile for example. You may need to do some extra work to get your IDE to pick it up.

export ANDROID_HOME=/path/to/android/sdk
android.sdk.path system property

Set the Java system property android.sdk.path, e.g. by putting -Dandroid.sdk.path=/path/to/android/sdk on the command line.

which android

As a last resort, Robolectric will try running which android to find the executable on your path. Add the SDK tools to your path:

PATH=/path/to/android/sdk/tools:$PATH

Type com.google.android.maps.MapView not present

java.lang.TypeNotPresentException: Typecom.google.android.maps.MapView not present at org.robolectric.Robolectric.bindShadowClass(Robolectric.java:67) Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for com.google.android.maps.MapView ...
  1. Make sure you have the Google Maps API jar in your build path.
  2. Even if you’re building against an earlier version of the API, link Robolectric to version 7 or higher.