Thursday, May 9, 2013

Announcing the Robolectric 2.0 release (candidate)!

We're very, very pleased to announce that Robolectric 2.0 is now feature complete and nearly ready to be minted (note the subtle lamb pun there)!

Thank you to Square for giving us Day 4 of the Seven Days of Open Source campaign of free software awesomeness: Announcing Robolectric 2.0 RC.

Robolectric has a new home!


To give us more flexible management of access for community contributors, Robolectric is moving to its own GitHub org.

From now on you'll find the main Robolectric GitHub project here: http://github.com/robolectric/robolectric

You can update your git repos with this command:
$ git remote set-url origin git://github.com/robolectric/robolectric.git

Robolectric has a new home, but the folks behind it remain the same: big thanks to Pivotal Labs, Square, and other backers for time and support!

Wednesday, May 8, 2013

Robolectric 2.0 alpha 3


I'm happy to announce the third alpha version of Robolectric 2!

We're now feature-complete on Robolectric 2.0! As before, this release is alpha-quality, and targeted at early adopters. We expect that there'll be bugs. APIs may change somewhat before final release. Please use this alpha only if you can deal with some churn.

Big thanks to Square and Pivotal for giving us time to work on this.


Changes since alpha 2:

  • Massive realism upgrade: we removed many, many shadows, particularly for views and widgets, so they'll behave much more realistically.
  • Dramatically improved resource loading, with support for themes and styles.
  • Easier TestRunner setup and configuration.
  • The new @Config annotation to specify config for a test class or test method:
    • The SDK level to report: @Config(reportSdk=Build.VERSION_CODES.FROYO)
    • Qualifiers for resource loading: @Config(qualifiers="fr-port-hdpi")
    • Custom shadow classes to enable.
  • For classes and methods that aren't explicitly shadowed, Robolectric now calls through to the real SDK code.
  • Views now complain if you try to pass in a null Context. Don't do it!
  • SDK method execution is now much much faster, especially in cases where we use the actual SDK code rather than a shadow (which will increasingly be the normal case).
  • Performance and memory use improvements.

    Changes coming soon:

    • Ability to switch Android SDK level on a per-test basis. The @Config annotation will allow you to specify which version of the SDK to emulate: @Config(emulateSdk=Build.VERSION_CODES.FROYO)
    • Performance and memory use improvements.

      Use it!

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

      If you're new to Robolectric, just add this to your pom:
      <dependency>
        <groupId>org.robolectric</groupId>
        <artifactId>robolectric</artifactId>
        <version>2.0-alpha-3</version>
      </dependency>
      Add a @RunWith to your test class and you're ready to go!
      @RunWith(RobolectricTestRunner.class)
      public class MyActivityTest {
        ...
      }

      Help:

      If you find bugs, make an issue on our github project and label it with robolectric-2.

      Friday, April 5, 2013

      The Test Lifecycle in 2.0

      Starting with Robolectric 2.0 alpha 3, there are some changes in how Robolectric prepares to run your tests.

      Finding Your Application

      By default, Robolectric first looks in your AndroidManifest.xml for the class to load as your application:

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.myapp">
        <application android:name=".Application"/>
      </manifest>

      That will cause a class called com.myapp.Application to be loaded. However, if you create a class with the same name and package, but with "Test" prepended to the class name, Robolectric will load that instead.

      package com.myapp;
      class TestApplication extends Application {
      }

      The Application Lifecycle

      Robolectric now automatically calls an application's onCreate() method before the test runs, and onTerminate() after the test runs. Note that this a change from earlier versions. If you want to prevent your application's onCreate() method from being called, you can override it in a test class and have it do nothing.

      If your test application implements the TestLifecycleApplication interface, it will get called with a few more useful events:

      class TestApplication extends Application
          implements TestLifecycleApplication {
        @Override public void beforeTest(Method method) {
        }

        @Override public void prepareTest(Object test) {
        }

        @Override public void afterTest(Method method) {
        }
      }

      The overall steps taken when running each test, then, are:
      1. Create your application.
      2. Call application.onCreate().
      3. Call application.beforeTest().
      4. Call application.prepareTest().
      5. Run the test.
      6. Call application.onTerminate().
      7. Call application.afterTest().

      These changes should make it possible for many people to avoid overriding RobolectricTestRunner altogether. Note that you can still change this by using your own subclass of RobolectricTestRunner, which provides a different TestLifecycle class.

      Tuesday, March 5, 2013

      Using ActionBarSherlock with Robolectric 2

      Hey everybody! You look great today!

      We're getting closer to seamless integration with ActionBarSherlock in Robolectric 2, but it's not quite there yet.

      To use the ActionBarSherlock library with our 2.0-alpha-2 release, you'll need to 


      Tuesday, February 26, 2013

      Robolectric 2.0 alpha 2

      I'm happy to announce the second alpha version of Robolectric 2!

      As before, this release is alpha-quality, and targeted at early adopters. We expect that there'll be bugs. We expect APIs to change before final release. Please use this alpha only if you can deal with some churn.

      Big thanks to Square and Pivotal for giving us time to work on this.


      Changes since alpha 1:

      • Classes in the android support lib are now fully enabled without the use of shadows.
      • Resource loading is better than ever. Library project support is much more robust.
      • Internally we've switched from Javassist to ASM for the bytecode manipulation that works Robolectric's magic. This and a couple other optimizations give us a huge performance win: one project's test suite here at Square dropped from 163 seconds to 76.
      • We report our Build.VERSION.SDK_INT as 8 (Froyo) for now.
      • Should get along well with ActionBarSherlock.


      Changes coming soon:

      • Removing even more shadows (most non-JNI Android classes should work without much shadowing) and enabling more of the SDK.
      • Easier TestRunner setup and configuration.
      • Performance and memory use improvements.
      • Ability to switch Android SDK level on a per-test basis.


      Use it!

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

      If you're new to Robolectric, just add this to your pom:
      <dependency>
        <groupId>org.robolectric</groupId>
        <artifactId>robolectric</artifactId>
        <version>2.0-alpha-2</version>
      </dependency>
      Add a @RunWith to your test class and you're ready to go!
      @RunWith(RobolectricTestRunner.class)
      public class MyActivityTest {
        ...
      }

      Caveats:

      Currently only android.support.v4's Fragments and LruCache are supported. Support for the core android Fragments and LruCache from later SDK versions will be coming soon. As a workaround, you should be able to use the support versions for now.


      Help:

      If you find bugs, make an issue on our github project and label it with robolectric-2.

      Migrating to Robolectric 2.0

      There are several changes in Robolectric 2.0 that will require modifications to existing projects that use the 1.x versions. Here are some quick notes on migrating your Robolectric 1.x-based test suite to 2.0-alpha-2.

      Update your pom.xml

      Robolectric's maven groupId changed from com.pivotallabs to org.robolectric:
        <dependency>
          <groupId>org.robolectric</groupId>
          <artifactId>robolectric</artifactId>
          <version>2.0-alpha-2</version>
        </dependency>

      Package rename

      Robolectric moved from com.xtremelabs.robolectric to org.robolectric. A simple search and replace in your code should do the trick:

      $ cd my-project-dir
      $ find . -name \*.java | xargs -n 10 \
        sed -i.bak 's/com\.xtremelabs\.robolectric/org.robolectric/g'


      Update your TestRunners

      If you've subclassed RobolectricTestRunner, you'll need to remove some constructors and override some different methods than before. RobolectricTestRunner and related configuration will be overhauled significantly for 2.0-alpha-3, so if you're doing extensive customization you might want to wait until then to upgrade.

      If that doesn't scare you off, here's a template for customizing the test runner in alpha-2:

      public class MyTestRunner extends RobolectricTestRunner {
        public MyTestRunner(Class<?> testClass) throws InitializationError {
          super(RobolectricContext.bootstrap(MyTestRunner.class, testClass,
              new RobolectricContext.Factory() {
                @Override
                public RobolectricContext create() {
                  return new RobolectricContext() {
                    // override methods here to customize things...
                  };
                }
              }));
        }
      }

      Specifying qualifiers

      In Robolectric 1.x, there were a few ways to goad it into loading resources other than the defaults, including calling setLayoutQualifierSearchPath() on a ViewLoader (and occasionally it worked). In Robolectric 2, the easiest way is to put a @Values annotation on your test:

      @Test @Values(qualifiers = "fr-land")
      public void shouldShowBucolicFrenchLandscape() throws Exception {
        // ...
      }