使用robolectric进行单元测试会产生NoSuchMethodException错误

时间:2015-08-01 05:25:56

标签: android unit-testing robolectric runtimeexception nosuchmethoderror

经过一段时间的研究后,我仍然没有发现为什么这个事情发生在我的Android代码中。这只是标准的简单单元测试代码,实际上还没有多少。我以前在我的工作机器上尝试过此代码并成功运行单元测试。我不知道为什么这会发生在我家里的其他电脑上。也许这可能是由于robolectric 3的一些错误设置造成的,我不太确定。我附上了完整的日志报告,也许你们能够更好地理解它。感谢。

Gradle命令运行单元测试:

gradlew test

错误:

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

完整日志:

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

这是我的mainActivityTest

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

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

这是我的build.gradle文件

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'
    }
}

我见过以下来源,第一个似乎是最接近的一个。我还在尝试怎么做。

2 个答案:

答案 0 :(得分:7)

就我而言,我编写了一个测试活动,它扩展了测试类中的正常活动。并遇到了这个错误。 我将测试活动从测试类移到了一个单独的文件中,然后就可以了。

答案 1 :(得分:2)

您好将SDK从18更改为19修复了错误。不确定是什么原因,但现在下面的代码工作。对于sdk 18来说,也许robolectric有依赖性问题。 谢谢@Eugen

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

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