简体   繁体   English

Robolectric中的资源NotFoundException

[英]Resources NotFoundException in Robolectric

Since I migrated a project to Android Studio, I am getting following error when I want to run my tests: 由于我将项目迁移到Android Studio,因此当我要运行测试时遇到以下错误:

WARNING: no system properties value for ro.build.date.utc
DEBUG: Loading resources for com.persistpalapp.android from ./../app/src/main/res...
java.lang.IllegalStateException: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e0064
    at com.persistpalapp.android.RPApplication.init(RPApplication.java:107)
    at com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83)
    at com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56)
    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:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e0064
    at android.content.res.Resources.getText(Resources.java:239)
    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:325)
    at com.persistpalapp.android.RPApplication.init(RPApplication.java:102)
    at com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83)
    at com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56)
    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:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    ... 1 more

java.lang.RuntimeException: java.lang.IllegalStateException: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e0064
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:226)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56)
    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:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.IllegalStateException: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e0064
    at com.persistpalapp.android.RPApplication.init(RPApplication.java:107)
    at com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83)
    at com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222)
    ... 19 more
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f0e0064
    at android.content.res.Resources.getText(Resources.java:239)
    at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:363)
    at android.content.res.Resources.getText(Resources.java)
    at android.content.res.Resources.getString(Resources.java:325)
    at com.persistpalapp.android.RPApplication.init(RPApplication.java:102)
    at com.persistpalapp.android.RPApplication.onCreate(RPApplication.java:83)
    at com.persistpalapp.android.TestRPApplication.onCreate(TestRPApplication.java:35)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:440)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:222)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:77)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:56)
    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:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    ... 1 more


Process finished with exit code 255

My testclass looks like this: 我的测试类如下所示:

@Config(manifest = C.MANIFEST, emulateSdk = 18, reportSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class JSONDataHelperTest {...}

Any ideas? 有任何想法吗?

================ MORE INFO ================== ===============更多信息==================

I am using 我在用

  • Android Studio 1.1 Beta 4 Android Studio 1.1 Beta 4
  • Roboletric 2.4 机器人2.4
  • com.android.tools.build:gradle:1.0.1 com.android.tools.build:gradle:1.0.1
  • com.github.jcandksolutions.gradle:android-unit-test:2.1.1 com.github.jcandksolutions.gradle:android-unit-test:2.1.1
  • org.robolectric:robolectric-gradle-plugin:0.12.+ org.robolectric:robolectric-gradle-plugin:0.12。+

My project structure (modules): 我的项目结构(模块):

  1. app: main app 应用:主应用
  2. robolectric-tests: testing classes robolectric-tests:测试类

My robolectric-tests build.gradle: 我的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
}

The error message looks like that no resources are found ;) explicit the content from your string.xml 错误消息看起来好像找不到资源;)从您的string.xml中显式显示内容

Lets start .. 开始吧 ..

com.github.jcandksolutions.gradle:android-unit-test:2.1.1 com.github.jcandksolutions.gradle:android-unit-test:2.1.1

This plugin support explicit to have your tests in the same module 该插件支持明确地将您的测试放在同一模块中

  • app 应用程式
    • src/main/java src / main / java
    • src/test/java src /测试/ java

For extra test module you must use a different plugin to support robolectric. 对于额外的测试模块,您必须使用其他插件来支持robolectric。 You can also have extra test modules with the jcandksolutions plugin but this would be more a application->library relationship instead of test->application/library. 您还可以使用jcandksolutions插件使用额外的测试模块,但这将更多是应用程序->库关系,而不是test-> application / library。

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 androidModule.android.applicationVariants.toList()。first()。javaCompile.classpath testCompile androidModule.android.applicationVariants.toList()。first()。javaCompile.outputs.files testCompile文件(androidModule.plugins.findPlugin(“ com。 android.application“)。getBootClasspath())

Nice try but I guess this will not give you all classes and resources. 不错的尝试,但我想这不会给您所有的课程和资源。 This lines aren't necessary eval which plugin you will use. 此行对于使用哪个插件不是必需的。

What now ... 现在怎么办 ...

Questions is how should you project structure look? 问题是您的项目结构应该如何? Is it important for you to have tests in a separate module? 在单独的模块中进行测试对您来说重要吗?

With separated test module use https://github.com/novoda/gradle-android-test-plugin . 对于单独的测试模块,请使用https://github.com/novoda/gradle-android-test-plugin For one module stay with jcandksolutions or you could also try the plugin from robolectric team https://github.com/robolectric/robolectric-gradle-plugin 对于使用jcandksolutions的一个模块,或者您也可以尝试来自robolectric团队的插件https://github.com/robolectric/robolectric-gradle-plugin

All plugins have examples how to use them: 所有插件都有使用示例:

For 1.0 version from Android Studio I recommend jcandksolutions, because of much better integration into Android Studio. 对于Android Studio的1.0版本,我建议使用jcandksolutions,因为可以更好地集成到Android Studio中。

With Android Studio 1.1 Beta you can avoid any of this plugins, or use the current master from robolectric team. 借助Android Studio 1.1 Beta,您可以避免使用任何此类插件,也可以使用robolectric团队的最新版本。

Here is a example without plugins https://github.com/nenick/AndroidStudioAndRobolectric and a guide how you do it self step by step http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html 这是一个不带插件https://github.com/nenick/AndroidStudioAndRobolectric的示例,以及一个指导您如何逐步实现自我的指南http://nenick-android.blogspot.de/2015/02/android-studio-110- beta-4-and.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Robolectric和风味的Resources $ NotFoundException - Resources$NotFoundException with Robolectric and Flavors 测试中的Gradle Robolectric资源NotFoundException - Gradle Robolectric Resources NotFoundException in Testing Robolectric Resources $ NotFoundException与getBoolean()和getInteger() - Robolectric Resources$NotFoundException with getBoolean() and getInteger() Resources $ NotFoundException带Robolectric的字符串数组 - Resources$NotFoundException for String array with Robolectric 使用带有Gradle的Robolectric时的资源$ NotFoundException - Resources$NotFoundException when using Robolectric with Gradle 用robolectric运行单元测试get Resources $ NotFoundException - Run unit test with robolectric get Resources$NotFoundException 调用Robolectric.buildActivity()时出现Resources $ NotFoundException - Resources$NotFoundException when calling Robolectric.buildActivity() Robolectric Resources$NotFoundException 找不到库 (aar) 资源 - Robolectric Resources$NotFoundException can't find library (aar) resources Robolectric 2.4中的android.content.res.Resources $ NotFoundException - android.content.res.Resources$NotFoundException in Robolectric 2.4 Robolectric 使用导航架构组件,Resources$NotFoundException: nav_graph - Robolectric using Navigation Architecture Component,Resources$NotFoundException: nav_graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM