Tuesday, February 26, 2013

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:

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() {
          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 {
  // ...


  1. Hi,
    I tried to use version 2.0-alpha-2 and i receive the following error:
    java.lang.NoSuchMethodError: org.objectweb.asm.commons.GeneratorAdapter.(ILorg/objectweb/asm/MethodVisitor;ILjava/lang/String;Ljava/lang/String;)V
    at org.robolectric.bytecode.AsmInstrumentingClassLoader$MyGenerator.(AsmInstrumentingClassLoader.java:246)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader$ClassInstrumentor.redirectorMethod(AsmInstrumentingClassLoader.java:581)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader$ClassInstrumentor.instrumentConstructor(AsmInstrumentingClassLoader.java:437)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader$ClassInstrumentor.instrument(AsmInstrumentingClassLoader.java:353)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.getInstrumentedBytes(AsmInstrumentingClassLoader.java:208)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.findClass(AsmInstrumentingClassLoader.java:138)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:86)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at j

  2. I tried to use version 2.0-alpha-2 and it crashed with:
    java.lang.NoSuchMethodError: org.objectweb.asm.commons.GeneratorAdapter.(ILorg/objectweb/asm/MethodVisitor;ILjava/lang/String;Ljava/lang/String;)V
    at org.robolectric.bytecode.AsmInstrumentingClassLoader$MyGenerator.(AsmInstrumentingClassLoader.java:246)

    When I use version 2.0-alpha-1 the test loads and starts to run but I crashes on the issue with ActionBarSherlock not dealing with Build.VERSION.SDK_INT=0.
    I tried to use "RobolectricTestRunner.setStaticValue(Build.VERSION.class, "SDK_INT", 15) ;" but RobolectricTestRunner.setStaticValue() isn't available.

    Any ideas?


  3. Can you please provide a sample for MyTestRunner in alpha-3?