Robolectric 3.0 in Android Studio

2,512 views
Skip to first unread message

Felipe Silva

unread,
Feb 12, 2015, 8:53:18 PM2/12/15
to robol...@googlegroups.com
Hi everyone,

I have built Robolectric 3.0 SNAPSHOT on my local machine using scripts/install-robolectric.sh
This created the robolectric-3.0-SNAPSHOT-jar-with-dependencies.jar
Now I want to use it on Android Studio to run my tests. I will also need to add this jar to version control.
I have modified my build.gradle to this:


buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.0.2'
    }
}

dependencies {
    compile 'com.google.guava:guava:18.0'
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.squareup.dagger:dagger:1.2.1'
    provided 'com.squareup.dagger:dagger-compiler:1.2.1'

    testCompile 'junit:junit:4.11'
    testCompile 'org.assertj:assertj-core:1.7.1'
    testCompile 'org.mockito:mockito-core:1.9.5'
    testCompile 'com.squareup:fest-android:1.0.8'
    testCompile files('libs/robolectric-3.0-SNAPSHOT-jar-with-dependencies.jar')
}



But when I try to run the tests, I get the following error:


java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchMethodException: android.os.Looper.<init>(boolean)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:227)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:167)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodException: android.os.Looper.<init>(boolean)
at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:233)
at org.robolectric.shadows.ShadowLooper.createLooper(ShadowLooper.java:43)
at org.robolectric.shadows.ShadowLooper.access$000(ShadowLooper.java:25)
at org.robolectric.shadows.ShadowLooper$1.create(ShadowLooper.java:37)
at org.robolectric.shadows.ShadowLooper$1.create(ShadowLooper.java:35)
at org.robolectric.util.SoftThreadLocal$1.initialValue(SoftThreadLocal.java:8)
at org.robolectric.util.SoftThreadLocal$1.initialValue(SoftThreadLocal.java:6)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
at java.lang.ThreadLocal.get(ThreadLocal.java:170)
at org.robolectric.util.SoftThreadLocal.get(SoftThreadLocal.java:13)
at org.robolectric.shadows.ShadowLooper.resetThreadLoopers(ShadowLooper.java:55)
at org.robolectric.Shadows.reset(Shadows.java:1384)
at org.robolectric.Robolectric.reset(Robolectric.java:20)
at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:40)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:216)
... 35 more
Caused by: java.lang.NoSuchMethodException: android.os.Looper.<init>(boolean)
at java.lang.Class.getConstructor0(Class.java:3074)
at java.lang.Class.getDeclaredConstructor(Class.java:2170)
at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:219)
... 49 more


Do I need to add anything else to build.gradle?

Thanks,
Felipe

Felipe Henriques da Silva

unread,
Feb 13, 2015, 1:51:13 AM2/13/15
to robol...@googlegroups.com
I found the root cause of the problem.

Well, from the stacktrace it was clear that Robolectric was not finding the correct android.jar. But since it should download all dependencies at runtime, I was not sure about why.
The problem is that to emulate a clean environment, I deleted the .m2 folder. But robolectric had previously downloaded the dependencies, so it was not downloading them again at runtime. While debugging the TestRunner I found out why: the java environment cache folder was still there. Deleting this folder made Robolectric start downloading the dependencies at runtime again. Should this temp cache folder be deleted somehow?

Thanks,
Felipe
 

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/le1oJKpIW9Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Naoya Makino

unread,
Mar 25, 2015, 2:46:07 PM3/25/15
to robol...@googlegroups.com
Hey Felipe, did you figure out the problem? I am getting the exact same error.

Felipe Henriques da Silva

unread,
Mar 25, 2015, 3:01:50 PM3/25/15
to robol...@googlegroups.com
Yes, the cause is described in my last email, as well as the solution. You need to delete the cached java folder.

Naoya Makino

unread,
Mar 25, 2015, 4:02:48 PM3/25/15
to robol...@googlegroups.com
sorry could you describe how I can delete the cached java folder? I am a Mac user if that helps

Felipe Henriques da Silva

unread,
Mar 25, 2015, 4:26:04 PM3/25/15
to robol...@googlegroups.com
I am on a mac as well, but this cached folder is not fixed. If you can debug robolectric, the fastest way is to put a breakpoint on the TestRunner and check the name of the cached folder it is using.

Naoya Makino

unread,
Mar 25, 2015, 8:09:05 PM3/25/15
to robol...@googlegroups.com
thanks for the reply, Felipe. Would you mind if you could describe how you could debug robolectric? and May I see where you found your cached folder? it might give me a clue on where the cached folder can be located.

much appreciated your help.

Felipe Henriques da Silva

unread,
Mar 25, 2015, 10:40:24 PM3/25/15
to robol...@googlegroups.com
Try this:

- Go to /var/folders/
- run "find . -name robolectric"
It will return something like this:
./bk/f9zdkyjn08s852pg8ly16lqsf9s8yp/T/robolectric
Remove this folder and any file/folder with "robolectric" in its name.
Now run your tests.

Naoya Makino

unread,
Mar 26, 2015, 12:42:19 PM3/26/15
to robol...@googlegroups.com
Hi Felipe, thank you so much for your help. I really appreciate your help.
I was able to delete the temp files related to `robolectric` based on your description. now I am getting the original problem.
```
[INFO] Unable to find resource 'org.robolectric:shadows-core:jar:21:3.0-SNAPSHOT' in repository sonatype (https://oss.sonatype.org/content/groups/public/)
    Unable to resolve artifact: Missing:
    ----------
    1) org.robolectric:shadows-core:jar:21:3.0-SNAPSHOT

      Try downloading the file manually from the project website.

      Then, install it using the command:
          mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.0-SNAPSHOT -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

      Alternatively, if you host your own repository you can deploy the file there:
          mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.0-SNAPSHOT -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

      Path to dependency:
      1) org.apache.maven:super-pom:pom:2.0
      2) org.robolectric:shadows-core:jar:21:3.0-SNAPSHOT

    ----------
    1 required artifact is missing.

    for artifact:
      org.apache.maven:super-pom:pom:2.0

    from the specified remote repositories:

        Caused by:
        org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException: Missing:
        ----------
        1) org.robolectric:shadows-core:jar:21:3.0-SNAPSHOT

          Try downloading the file manually from the project website.

          Then, install it using the command:
              mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.0-SNAPSHOT -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file

          Alternatively, if you host your own repository you can deploy the file there:
              mvn deploy:deploy-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.0-SNAPSHOT -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

          Path to dependency:
          1) org.apache.maven:super-pom:pom:2.0
          2) org.robolectric:shadows-core:jar:21:3.0-SNAPSHOT

        ----------
        1 required artifact is missing.

        for artifact:
          org.apache.maven:super-pom:pom:2.0

        from the specified remote repositories:
          central (http://repo1.maven.org/maven2)

```

I got this problem before and I manually installed shadows-core-3.0-20150324.035017-318-21.jar from https://oss.sonatype.org/content/groups/public/org/robolectric/shadows-core/3.0-SNAPSHOT/ with the command 
`mvn install:install-file -DgroupId=org.robolectric -DartifactId=shadows-core -Dversion=3.0-SNAPSHOT -Dclassifier=21 -Dpackaging=jar -Dfile=/path/to/file`
and got to this problem of ` java.lang.RuntimeException: java.lang.NoSuchMethodException: android.os.Looper`. I am not quite sure what the root cause of this issue is. Do you see anything? I really appreciate your help on this.

here is my gradle related to robolectric
buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.14.+'
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
    }
}

apply plugin: 'robolectric'
repositories {
    mavenCentral()
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
    }
}
dependencies {
...
    androidTestCompile('org.robolectric:robolectric:3.0-SNAPSHOT') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
        exclude group:'org.hamcrest'
    }

bme...@twitter.com

unread,
Apr 10, 2015, 6:43:11 PM4/10/15
to robol...@googlegroups.com
Amazing find, Felipe!

I had exactly the same problem, for exactly the same reason.  Tried to create a clean env, and as soon as I did, got failures.  Thanks a million.

-blake 

André da Silva Medeiros

unread,
Apr 14, 2015, 8:36:53 AM4/14/15
to robol...@googlegroups.com
Hi Felipe,

I am facing this very problem, but on a windows machine and cannot find such "T/roboeletric" folder. I have deleted folders on "C:\Users\Develop\.gradle\caches" but had no success.

André da Silva Medeiros

unread,
Apr 15, 2015, 1:52:35 PM4/15/15
to robol...@googlegroups.com
I just found out. In my maven settings.xml I added (as specified in maven site)):

<localRepository>${user.home}/.m2/repository</localRepository>

After remove it Roboletric works fine.

Jens Driller

unread,
May 14, 2015, 6:18:11 AM5/14/15
to robol...@googlegroups.com
Thanks Felipe, worked like a charm! :)
Reply all
Reply to author
Forward
0 new messages