frey
frey

Reputation: 481

Unit testing with robolectric gives NoSuchMethodException error

after some time of research I still haven't found out why this thing happens in my Android code. This is just standard simple unit test code, Not much is really going on. I tried this code previously on my work machine and successfully runs the unit test. I don't know why this happens at my other pc at home. Maybe it could be caused by some incorrect setup of robolectric 3, I'm not really sure. I attached the full log report maybe you guys will be able to understand it better. Thanks.

Gradle command to run unit test:

gradlew test

Error:

com.sample.bigger.MainActivityTest > sampleTest FAILED
java.lang.RuntimeException                              
    Caused by: java.lang.RuntimeException               
        Caused by: java.lang.NoSuchMethodException      

Full log:

java.lang.RuntimeException: java.lang.NoSuchMethodException: android.os.Looper.<init>(boolean)
    at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:297)
    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:13)
    at org.robolectric.util.SoftThreadLocal$1.initialValue(SoftThreadLocal.java:11)
    at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:160)
    at java.lang.ThreadLocal.get(ThreadLocal.java:150)
    at org.robolectric.util.SoftThreadLocal.get(SoftThreadLocal.java:18)
    at org.robolectric.shadows.ShadowLooper.resetThreadLoopers(ShadowLooper.java:55)
    at org.robolectric.Shadows.reset(Shadows.java:1612)
    at org.robolectric.Robolectric.reset(Robolectric.java:22)
    at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:43)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:233)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: android.os.Looper.<init>(boolean)
    at java.lang.Class.getConstructor0(Class.java:2892)
    at java.lang.Class.getDeclaredConstructor(Class.java:2058)
    at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:283)
    ... 48 more

This is my mainActivityTest

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 18)
public class MainActivityTest {

@Test
    public void sampleTest(){
        Assert.assertEquals("hey", "hey");
    }
}

This is my build.gradle file

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.sample.bigger"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // Added for AdMob
    compile project(':jokeproviderlib')
    compile project(':jokedisplayer')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.3.0'
    compile project(path: ':jokeGCMbackend', configuration: 'android-endpoints')
    compile 'com.google.mockwebserver:mockwebserver:20130706'


    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"
    testCompile('org.robolectric:robolectric:3.0') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}

I have seen the following sources, the first seems to be the closest one. I'm still trying how to do it.

Upvotes: 4

Views: 4409

Answers (2)

Chris.Zou
Chris.Zou

Reputation: 4576

In my case, I wrote a testing activity which extends the normal activity inside the test class. and ran into this error. I moved the testing activity out from the test class, to a separate file, and then it worked out fine.

Upvotes: 8

frey
frey

Reputation: 481

Hello changing the SDK from 18 to 19 fixed the error. Not really sure what is the cause but the code below now works. Maybe robolectric had dependency trouble when it comes to sdk 18. Thanks @Eugen

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
public class MainActivityTest {

@Test
    public void sampleTest(){
        Assert.assertEquals("hey", "hey");
    }
}

Upvotes: 4

Related Questions