android.content.res.Resources$NotFoundException at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:228) using `org.robolectric:robolectric:3.0-rc`

1,162 views
Skip to first unread message

Naoya Makino

unread,
Apr 15, 2015, 8:17:25 PM4/15/15
to robol...@googlegroups.com
Hi there, I've been trying to get robolectric tests running with 3.0-rc2 and having no luck so far.
I used to be able to build robolectric tests just fine when I was using `androidTestCompile('org.robolectric:robolectric:3.0-SNAPSHOT')`
but I faced this issue https://github.com/robolectric/robolectric/issues/1536#issuecomment-87638415; it seemed like 3.0-SNAPSHOT was a bad build, so I tried with 3.0-rc2 build, as recommended here: https://github.com/robolectric/robolectric/issues/1643

However, I am seeing these errors every time I try to tun the tests

java.lang.RuntimeException: android.content.res.Resources$NotFoundException: unknown resource 2131165228
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:228)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	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: android.content.res.Resources$NotFoundException: unknown resource 2131165228
	at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:332)
	at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:70)
	at android.content.res.AssetManager.getResourceText(AssetManager.java)
	at android.content.res.Resources.getText(Resources.java:270)
	at org.robolectric.shadows.ShadowResources.getText(ShadowResources.java:361)
	at android.content.res.Resources.getText(Resources.java)
	at android.content.res.Resources.getString(Resources.java:360)
	at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:39)
	at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:69)
	at android.content.Context.getString(Context.java)
	at com.themis.clioAndroid.authentication.AccountUtils.getAccounts(AccountUtils.java:85)
	at com.themis.clioAndroid.authentication.AccountUtils.isAccountAvailable(AccountUtils.java:50)
	at com.themis.clioAndroid.testhelper.TestApplicationModule.provideAccountUtils(TestApplicationModule.java:33)
	at com.themis.clioAndroid.testhelper.TestApplicationModule$$ModuleAdapter$ProvideAccountUtilsProvidesAdapter.get(TestApplicationModule$$ModuleAdapter.java:62)
	at com.themis.clioAndroid.testhelper.TestApplicationModule$$ModuleAdapter$ProvideAccountUtilsProvidesAdapter.get(TestApplicationModule$$ModuleAdapter.java:46)
	at dagger.internal.Linker$SingletonBinding.get(Linker.java:364)
	at com.themis.clioAndroid.ClioAppLifecycleHandler$$InjectAdapter.injectMembers(ClioAppLifecycleHandler$$InjectAdapter.java:74)
	at com.themis.clioAndroid.ClioAppLifecycleHandler$$InjectAdapter.get(ClioAppLifecycleHandler$$InjectAdapter.java:63)
	at com.themis.clioAndroid.ClioAppLifecycleHandler$$InjectAdapter.get(ClioAppLifecycleHandler$$InjectAdapter.java:23)
	at dagger.internal.loaders.ReflectiveAtInjectBinding.injectMembers(ReflectiveAtInjectBinding.java:120)
	at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:281)
	at com.themis.clioAndroid.ClioApp.inject(ClioApp.java:119)
	at com.themis.clioAndroid.ClioApp.onCreate(ClioApp.java:58)
	at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)
	at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:431)
	at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:224)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	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 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)
	... 7 more

I am losing ideas on why I can no longer build Robolectric tests.
Here is my build.gradle with relevant to Robolectric.

buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'org.robolectric:robolectric-gradle-plugin:0.14.+'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
}
}
apply plugin: 'robolectric'

repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
}

android {

compileSdkVersion 21
buildToolsVersion '21.1.2'

defaultConfig {
minSdkVersion 16
targetSdkVersion 21

}
}

dependencies {
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services-base:6.5.+'
compile "com.android.support:appcompat-v7:21.0.3"

androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'com.squareup.dagger'
exclude group: 'com.google.guava'
}
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}

androidTestCompile('org.robolectric:shadows-support-v4:3.0-rc1') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}

androidTestProvided('org.mockito:mockito-all:1.9.5')
}

robolectric {
// configure the set of classes for JUnit tests
include '**/*Test.class'
exclude '**/espresso/**/*.class'

// configure max heap size of the test JVM
maxHeapSize = '2048m'

// configure the test JVM arguments
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'

// Specify max number of processes (default is 1)
// set environment variable MAX_PARALLEL_FORKS=true
maxParallelForks = System.getenv("MAX_PARALLEL_FORKS") ? 4 : 2

// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150

// configure whether failing tests should fail the build
ignoreFailures true

// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}

 
And in the tests, I have the following test runner.
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)

Do you see any reasons why I am getting ResourcesNotFoundException?
FYI: it happens even after deleted caches from `~/.m2` and `~/.gradle/caches/`.
any suggestions are welcome.

cheers

Johan Bilien

unread,
Apr 16, 2015, 1:26:44 PM4/16/15
to robol...@googlegroups.com
I'm having a similar issue with gradle 1.1, robolectric 3.0-rc2. No matter what I try the tests fail to resolve any resources.

I run the tests with:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21, manifest = "fire-drill/AndroidManifest.xml")


Not sure if this is related but our project doesn't use the standard directory structure, so we have:

sourceSets
{
 main
{
 manifest
.srcFile 'fire-drill/AndroidManifest.xml'
 java
.srcDirs = ['fire-drill/src']
 resources
.srcDirs = ['fire-drill/src']
 aidl
.srcDirs = ['fire-drill/src']
 renderscript
.srcDirs = ['fire-drill/src']
 res
.srcDirs = ['fire-drill/res']
 assets
.srcDirs = ['fire-drill/assets']
 
}

 test
{
 java
.srcDirs = ['src/test/java']
 res
.srcDirs = ['fire-drill/res']
 
}

 androidTest
{
 java
.srcDirs = ['fire-drill-tests/src']
 resources
.srcDirs = ['fire-drill-tests/src']
 aidl
.srcDirs = ['fire-drill-tests/src']
 renderscript
.srcDirs = ['fire-drill-tests/src']
 res
.srcDirs = ['fire-drill-tests/res']
 assets
.srcDirs = ['fire-drill-tests/assets']
 
}

 debug
{
 manifest
.srcFile 'fire-drill/dev/AndroidManifest.xml'
 res
.srcDirs = ['fire-drill/dev/res']
 
}

 stage
{
 manifest
.srcFile 'fire-drill/stage/AndroidManifest.xml'
 res
.srcDirs = ['fire-drill/stage/res']
 
}

 release
{
 manifest
.srcFile 'fire-drill/release/AndroidManifest.xml'
 
}
}


in the build.gradle.

Erich Douglass

unread,
Apr 16, 2015, 2:11:17 PM4/16/15
to robol...@googlegroups.com
A few things:

- I would suggest upgrading 'com.android.tools.build:gradle' to version 1.1.0 since it has built-in support for unit tests.
- Remove 'org.robolectric:robolectric-gradle-plugin', since it isn't needed with version 1.1.0 of the android gradle plugin. This means that you can also remove the giant block of robolectric configuration at the end of your build.gradle. Also, chance the scope from 'androidTestCompile' to just 'testCompile' for the robolectric artifacts.
- You are using version '3.0-rc1' of 'org.robolectric:shadows-support-v4'. This should be upgraded to version '3.0-rc2' to match your version of robolectric.

Try making those changes and see if it helps. You can also check out the 'robolectric/robolectric-samples' repo on github if you want to see some more working examples.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Erich Douglass
Engineering Manager

Erich Douglass

unread,
Apr 16, 2015, 2:13:44 PM4/16/15
to robol...@googlegroups.com
Johan,

I haven't tried running robolectric in a project that has that sort of layout. For what it's worth, 'RobolectricGradleTestRunner' ignores the 'manifest' property. Also, you don't need to specify 'emulateSdk' unless you're building against API 22.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Naoya Makino

unread,
Apr 17, 2015, 6:52:42 PM4/17/15
to robol...@googlegroups.com
Hi Eric, thanks for your reply. I made changes according to your suggestions. few changes: espresso is now migrated to 2.0. got rid of robolectric plugin and related code, made shadows-support-v4 to 3.0-rc2. and changed the file structures.





































































































buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
}
    dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
}
}

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'


repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
}

android {

compileSdkVersion 21
buildToolsVersion '21.1.2'

defaultConfig {
minSdkVersion 16
targetSdkVersion 21
        testInstrumentationRunner "com.themis.clioAndroid.ClioTestRunner"
}

buildTypes {
qa {
debuggable true
applicationIdSuffix '.qa'
}
debug {
applicationIdSuffix '.debug'
}
}

// This is important, it will run lint checks but won't abort build
lintOptions {
abortOnError false
}

// Work around for http://stackoverflow.com/questions/17320088/android-build-dex-jumbo-mode-in-gradle
dexOptions {
jumboMode = true
}
}

dependencies {
compile 'com.squareup.dagger:dagger:1.2.1'
// dependency injection library http://square.github.io/dagger/
provided 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.android.support:support-v4:21.0.3'
compile "com.android.support:appcompat-v7:21.0.3"
// App's dependencies, including test
compile 'com.android.support:support-annotations:21.0.3'

//Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

// Robolectric
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.hamcrest:hamcrest-integration:1.1'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile('org.robolectric:robolectric:3.0-rc2') {

exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}

    testCompile('org.robolectric:shadows-support-v4:3.0-rc2') {

exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}

    testCompile('org.mockito:mockito-all:1.9.5')
}


I also noticed that R file is not generated for a package under `build/generated/source/r/androidTest/` nor there is `test` folder under `build/generated`. It seems like resources are not generated for my `src/test/*`. Do you see a problem why it could cause such an issue?

Reply all
Reply to author
Forward
0 new messages