Android.content.res.Resources $ NotFoundException in Robolectric 2.4

I'm having problems loading resources when running unit tests from the command line. It works great at IntelliJ.

I use:

  • com.android.tools.build: gradle: 1.1.3
  • org.robolectric: robolectric- gradle -plugin: 1.0.1
  • org.robolectric: robolectric: 2.4

I have the following project structure (multi-module):

+---module1 | | build.gradle | \---src | +---main | | | AndroidManifest.xml | | | | | +---res | | | \---values | | | strings.xml | \---test | \---java | \---example | | FooTest.java +---module2 etc 

My test (simplified) is as follows:

 @Config(emulateSdk = 18, reportSdk = 18, manifest = "./src/main/AndroidManifest.xml") @RunWith(RobolectricTestRunner.class) public class FooTest { @Test public void test() { String result = Robolectric.application.getString(R.string.error_message); assertThat(result, notNullValue()); } } 

I get this stack:

 android.content.res.Resources$NotFoundException: unknown resource 2131361826 at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:311) at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:69) at android.content.res.AssetManager.getResourceText(AssetManager.java) at android.content.res.Resources.getText(Resources.java:235) 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 org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:41) at org.robolectric.shadows.ShadowContextWrapper.getString(ShadowContextWrapper.java:96) at android.content.Context.getString(Context.java) 

Note I am new to Gradle, so there is most likely an incorrect configuration. I suggested that, following the agreement on the project structure, I do not need to indicate where to look for resources. I tried to point them out, but no luck:

 android { sourceSets { main { manifest.srcFile 'src/main/AndroidManifest.xml' java.srcDirs = ['src/main/java'] res.srcDirs = ['src/main/res'] assets.srcDirs = ['src/main/assets'] } } } 
+6
source share
1 answer

During the study, we found the cause of the problem. If you use the android gradle plugin v1.1.x , you do not need a generic robolectric gradle plugin .

Make sure that the incorrect version of the robolectric gradle plugin your tests.

+1
source

Source: https://habr.com/ru/post/984886/


All Articles