robolectric 的单元测试给出 NoSuchMethodException 错误
Unit testing with robolectric gives NoSuchMethodException error
经过一段时间的研究,我仍然没有发现为什么我的 Android 代码中会出现这种情况。这只是标准的简单单元测试代码,真正发生的事情并不多。我之前在我的工作机器上试过这段代码并成功地 运行s 了单元测试。我不知道为什么这会发生在我家里的另一台电脑上。也许这可能是由 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[=13=]0(ShadowLooper.java:25)
at org.robolectric.shadows.ShadowLooper.create(ShadowLooper.java:37)
at org.robolectric.shadows.ShadowLooper.create(ShadowLooper.java:35)
at org.robolectric.util.SoftThreadLocal.initialValue(SoftThreadLocal.java:13)
at org.robolectric.util.SoftThreadLocal.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner.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.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'
}
}
我看过以下来源,第一个似乎是最接近的。我还在尝试怎么做。
- https://groups.google.com/forum/#!topic/robolectric/le1oJKpIW9Q
- Robolectric 3.0 java.lang.NoSuchMethodException on create activity
- nosuchmethoderror-cannot-be-cast-to-运行timeexception-when-creati
- https://github.com/robolectric/robolectric/issues/1633
您好,将 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");
}
}
在我的例子中,我写了一个测试 activity,它扩展了测试 class 中的正常 activity。和 运行 进入这个错误。
我将测试 activity 从测试 class 中移出到一个单独的文件中,然后它运行良好。
经过一段时间的研究,我仍然没有发现为什么我的 Android 代码中会出现这种情况。这只是标准的简单单元测试代码,真正发生的事情并不多。我之前在我的工作机器上试过这段代码并成功地 运行s 了单元测试。我不知道为什么这会发生在我家里的另一台电脑上。也许这可能是由 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[=13=]0(ShadowLooper.java:25)
at org.robolectric.shadows.ShadowLooper.create(ShadowLooper.java:37)
at org.robolectric.shadows.ShadowLooper.create(ShadowLooper.java:35)
at org.robolectric.util.SoftThreadLocal.initialValue(SoftThreadLocal.java:13)
at org.robolectric.util.SoftThreadLocal.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner.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.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'
}
}
我看过以下来源,第一个似乎是最接近的。我还在尝试怎么做。
- https://groups.google.com/forum/#!topic/robolectric/le1oJKpIW9Q
- Robolectric 3.0 java.lang.NoSuchMethodException on create activity
- nosuchmethoderror-cannot-be-cast-to-运行timeexception-when-creati
- https://github.com/robolectric/robolectric/issues/1633
您好,将 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");
}
}
在我的例子中,我写了一个测试 activity,它扩展了测试 class 中的正常 activity。和 运行 进入这个错误。 我将测试 activity 从测试 class 中移出到一个单独的文件中,然后它运行良好。