gpt4 book ai didi

android - 使用 robolectric 进行单元测试会出现 NoSuchMethodException 错误

转载 作者:行者123 更新时间:2023-12-02 12:06:54 25 4
gpt4 key购买 nike

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

这是我的主要ActivityTest

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

我看过以下来源,第一个似乎是最接近的。我还在尝试如何做到这一点。

最佳答案

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

关于android - 使用 robolectric 进行单元测试会出现 NoSuchMethodException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31758568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com