2015-02-12 3 views
2

Поскольку я мигрировал проект в Android Studio, я получаю следующее сообщение об ошибке, когда я хочу запустить мои тесты:Ресурсы NotFoundException в Robolectric

 
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 

Мой TestClass выглядит следующим образом:

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

Любой идеи?

================ ПОДРОБНЕЕ ==================

Я использую

  • Android-студия 1.1 Beta 4
  • Roboletric 2,4
  • com.android.tools.build:gradle:1.0.1
  • com.github.jcandksolutions.gradle: Android-блок-тест: 2.1.1
  • org.robolectric: robolectric-Gradle-плагин:. 0,12 +

Моя структура проекта (модули):

  1. приложение: главный приложение
  2. robolectric-тесты: тестирование классов

Мои robolectric-тесты 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 
} 
+0

к менее информации. какая у вас версия для Android? какой плагин вы используете для поддержки robolectric в студии andorid? как выглядит структура проекта? – nenick

+0

@nenick ok, добавлено больше информации – stoefln

+0

@stoefln Почему у вас так много «хаков» в вашем 'build.gradle'? –

ответ

0

Сообщение об ошибке выглядит так, что никакие ресурсы не найдены;) явное содержание от вашего string.xml

Позволяет начать ..

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

Этот плагин поддерживает явные тесты в одном модуле

  • приложение
    • SRC/главная/Java
    • SRC/тест/Java

Для дополнительного модуля тест вы должны использовать другой плагин для поддержки robolectric. У вас также могут быть дополнительные тестовые модули с плагином jcandksolutions, но это будет скорее связывание приложения->, а не test-> application/library.

testCompile androidModule.android.applicationVariants.toList(). Первый(). JavaCompile.classpath testCompile androidModule.android.applicationVariants.toList(). Первый(). JavaCompile.outputs.files testCompile файлы (androidModule.plugins.findPlugin ("com.android.application"). getBootClasspath())

Хорошая попытка, но я предполагаю, что это не даст вам все классы и ресурсы. Эти строки не нужны, какой плагин вы будете использовать.

Что теперь ...

Вопросы, как вы должны проецировать структуры внешний вид? Важно ли иметь тесты в отдельном модуле?

С изолированным испытательным модулем используйте https://github.com/novoda/gradle-android-test-plugin. Для одного модуля пребывания с jcandksolutions или вы могли бы также попробовать плагин от robolectric команды https://github.com/robolectric/robolectric-gradle-plugin

Все плагины имеют примеры, как использовать их:

Для версии 1.0 от Android Studio я рекомендую jcandksolutions, beca использование гораздо лучшей интеграции в Android Studio.

С Android Studio 1.1 Beta вы можете избежать любого из этих плагинов или использовать текущий мастер из команды robolectric.

Вот пример без плагинов https://github.com/nenick/AndroidStudioAndRobolectric и руководство, как вы делаете это самостоятельно шаг за шагом http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html

+0

Я пробовал с вашим одномодульным подходом (без плагинов), но в итоге оказался с той же проблемой (строковый ресурс не найден). Я также попытался запустить ваш пример проекта и не смог. Я не могу swtich «Test Artifact», см. Здесь: http://i.imgur.com/g2BYCnT.png – stoefln

+0

Вы следовали шагам 3 и 4 из этого руководства http://tools.android.com/tech- docs/unit-testing-support? – nenick

+0

Изображение похоже, что вы выбрали другой пример. Я не имею в виду https://github.com/nenick/android-gradle-template. Пример без плагинов - https://github.com/nenick/AndroidStudioAndRobolectric – nenick

Смежные вопросы