I've just set up Roboelectric 3.2.2 with a new app and I have written 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've followed all the set up and instructions here and here but I am still getting this error every time I try to run the test:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug]
This exception occurs at the first line of the test where I am calling setupActivity()
.
It worked for me: add
@Config(manifest = "src/main/AndroidManifest.xml")
Note 1: If you are still getting the same error (happens when creating new project)
Note 2: Using
Gradle
This plugin works for me when using custom RobolectricTestRunner and provide the missing system properties.
Edit: You must also use a different working directory at your unit test run configuration. Best is to change the default configuration and use $MODULE_DIR$ https://code.google.com/p/android/issues/detail?id=158015#c5