【发布时间】:2015-04-13 11:11:40
【问题描述】:
自从我将项目迁移到 Android Studio 后,当我想运行测试时出现以下错误:
警告:ro.build.date.utc 没有系统属性值 调试:从 ./../app/src/main/res 加载 com.persistpalapp.android 的资源... java.lang.IllegalStateException: android.content.res.Resources$NotFoundException: 字符串资源 ID #0x7f0e0064 在 com.persistpalapp.android.RPApplication.init(RPApplication.java:107) 在 com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83) 在 com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35) 在 org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126) 在 org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440) 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:606) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 引起:android.content.res.Resources$NotFoundException:字符串资源 ID #0x7f0e0064 在 android.content.res.Resources.getText(Resources.java:239) 在 org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363) 在 android.content.res.Resources.getText(Resources.java) 在 android.content.res.Resources.getString(Resources.java:325) 在 com.persistpalapp.android.RPApplication.init(RPApplication.java:102) 在 com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83) 在 com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35) 在 org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126) 在 org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440) 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) ... 1 更多 java.lang.RuntimeException: java.lang.IllegalStateException: android.content.res.Resources$NotFoundException: 字符串资源 ID #0x7f0e0064 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:226) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 原因:java.lang.IllegalStateException:android.content.res.Resources$NotFoundException:字符串资源 ID #0x7f0e0064 在 com.persistpalapp.android.RPApplication.init(RPApplication.java:107) 在 com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83) 在 com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35) 在 org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126) 在 org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440) 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222) ... 19 更多 引起:android.content.res.Resources$NotFoundException:字符串资源 ID #0x7f0e0064 在 android.content.res.Resources.getText(Resources.java:239) 在 org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363) 在 android.content.res.Resources.getText(Resources.java) 在 android.content.res.Resources.getString(Resources.java:325) 在 com.persistpalapp.android.RPApplication.init(RPApplication.java:102) 在 com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83) 在 com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35) 在 org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126) 在 org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440) 在 org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67) ... 1 更多 进程以退出代码 255 结束我的测试类如下所示:
@Config(manifest = C.MANIFEST, emulateSdk = 18, reportSdk = 18) @RunWith(RobolectricTestRunner.class) 公共类 JSONDataHelperTest {...}有什么想法吗?
================= 更多信息 ==================
我正在使用
- Android Studio 1.1 Beta 4
- 机器人 2.4
- com.android.tools.build:gradle:1.0.1
- com.github.jcandksolutions.gradle:android-unit-test:2.1.1
- org.robolectric:robolectric-gradle-plugin:0.12.+
我的项目结构(模块):
- 应用:主应用
- robolectric-tests:测试类
我的 robolectric-tests build.gradle:
evaluationDependsOn(':app')
apply plugin: 'java'
repositories {
mavenCentral()
def androidHome = System.getenv("ANDROID_HOME")
// check that androidHome points to the android home dir
println("android home dir: "+androidHome)
maven {
url "$androidHome/extras/android/m2repository/"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
def androidModule = project(':app')
compile androidModule
println "androidModule classpath: "+androidModule.android.applicationVariants.toList().first().javaCompile.classpath
testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath
testCompile androidModule.android.applicationVariants.toList().first().javaCompile.outputs.files
testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath())
//testCompile 'junit:junit:4.12-beta-2'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.mockito:mockito-core:1.10.8'
testCompile 'org.assertj:assertj-core:1.7.0'
testCompile 'com.squareup:fest-android:1.0.8'
}
//prevent the "superClassName is empty" error for classes not annotated as tests
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
【问题讨论】:
-
减少信息。你有哪个 android studio 版本?你用哪个插件来支持andorid studio中的robolectric?您的项目结构如何?
-
@nenick 好的,添加了更多信息
-
@stoefln 为什么你的
build.gradle中有这么多“黑客”? -
@JaredBurrows 因为我不知道如何包含我的主模块的类。测试类位于单独的库模块中。通常库不引用主应用程序。我复制了代码,但我真的很想知道为什么整个设置如此复杂。我花了几个小时(几乎是几天)调试。有点令人沮丧。
-
@stoefln 我刚刚找到你在哪里得到的
build.gradle。我知道 robolectric + 最新的 android gradle 构建工具很难设置。 Google 需要为 Robolectric 或他们自己的 Robolectric 版本提供支持。
标签: android testing robolectric