Wednesday, May 15, 2013

Robolectric 2.0 final!

Robolectric 2.0 is, at long last, finished! Thanks for all the bug reports over the last week.

We'll have some blog posts in the coming week talking about new features.

Use it!

If you've been using Robolectric 1.x, see the blog post on migrating to Robolectric 2.0.

If you're new to Robolectric, just add this to your pom:
Add a @RunWith to your test class and you're ready to go!
public class MyActivityTest {


If you find bugs, make an issue on our github project.


  1. How do you get the created from res Id for Drawables now? ShadowDrawable.getBitmap is gone. I was using this to get the res id from the ShadowBitmap.

  2. Nevermind, I got it:

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap);
    int loadedFromResourceId = shadowBitmap.getCreatedFromResId();

  3. The "migrating to Robolectric 2.0" blog post referenced above uses a class called RobolectricContext to configure the test runner, but RobolectricContext seems to not exist anymore in 2.0 final. It looks like there are two ways it can be done now:

    1. Use the standard RobolectricTestRunner, and define a properties file, where you can set the following properties: emulateSdk, manifest, qualifiers, reportSdk, and shadows. You can look at the source for org.robolectric.annotation.Config to figure out how these properties are used.

    2. Subclass RobolectricTestRunner, override getConfigProperties(), and return a Properties object that contains values for the properties mentioned in (1). This might be useful if for some reason you need to compute the values at runtime rather than have them hard coded in a properties file. There are other methods that can be overridden in RobolectricTestRunner but this seemed like the simplest and least invasive way to do it.

    Christian, it might be nice to provide a blog post with a sample properties file so people have an idea of what it should like.