使用Robolectric进行单元测试时出现NoSuchMethodException错误

4

经过一段时间的研究,我仍然没有找到为什么我的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

这是我的主Activity测试

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

我已经查阅了以下来源,第一个看起来似乎最接近。 我仍在尝试如何做到这一点。

1
我会移除mockwebserver依赖,因为你不想将其包含在应用程序中。其他所有内容看起来都是简单且有效的测试,应该可以运行。你能将SDK从18改为19吗?这样你就有相同的错误了吗? - Eugen Martynov
2个回答

8

我的情况是,我在测试类中编写了一个扩展常规活动的测试活动,并遇到了这个错误。我将测试活动从测试类移出到一个单独的文件中,然后问题得到了解决。


我曾经遇到过同样的问题。我将扩展活动的类保留在测试类中,但我将其更改为静态类。这也解决了问题。 - dazed

3

你好,将SDK从18更改为19解决了错误。不是很确定原因是什么,但下面的代码现在可以正常运行。也许Robolectric在处理SDK 18时存在依赖问题。 感谢@Eugen

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接