$ NotFoundException resources when running Roboelectric test

I just created Roboelectric 3.2.2 with a new application, and I wrote my first simple test:

@RunWith(RobolectricTestRunner.class) @Config(manifest="src/main/AndroidManifest.xml", packageName="my.pacakge.name.debug") public class MainActivityTest { @Test public void clickButton() { MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class); String text = ((TextView)mainActivity.findViewById(R.id.text_main)).getText().toString(); assertEquals("Should equal Hello World!", "Hello World!", text); } } 

I followed all the settings and instructions here and here , but I still get this error every time I try to run a test:

 android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug] 

This exception occurs in the first line of the test, where I call setupActivity() .

+6
android android-studio robolectric robolectric-gradle-plugin
source share
2 answers

This worked for me: add @Config(manifest = "src/main/AndroidManifest.xml")

 @Config(manifest = "src/main/AndroidManifest.xml") @RunWith(RobolectricTestRunner.class) public class MainActivityTest { @Test public void testSomething() throws Exception { assertTrue(Robolectric.setupActivity(MainActivityTest .class) != null); } } 

Note 1: If you still get the same error (happens when creating a new project)

The reason is related to a different working directory, and the answer is here: https://stackoverflow.com/a/167189/

Note 2: Use

Android Studio 3.0 (Canary 9)
Gradle 4.1
Android Gradle Plugin 3.0.0-alpha9
robolectric: 3.4.2

Gradle

 buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha9' } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' // Espresso androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' // Robolectric testImplementation 'org.hamcrest:hamcrest-library:1.3' testImplementation 'org.robolectric:robolectric:3.4.2' } 
+1
source share

This plugin works for me when using a custom RobolectricTestRunner and ensures there are no system properties.

 public class CustomRobolectricTestRunner extends RobolectricTestRunner { public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError { super(testClass); String buildVariant = BuildConfig.BUILD_TYPE + (BuildConfig.FLAVOR.isEmpty()? "" : "/" + BuildConfig.FLAVOR); System.setProperty("android.package", BuildConfig.APPLICATION_ID); System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml"); System.setProperty("android.resources", "build/intermediates/res/" + buildVariant); System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant); } } 

Edit: You should also use a different working directory in the unit test run configuration. It is best to change the default configuration and use $ MODULE_DIR $ https://code.google.com/p/android/issues/detail?id=158015#c5

0
source share

All Articles