我刚刚使用新应用程序设置了Roboelectric 3.2.2,并且我编写了第一个简单测试:
@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);
}
}
我已按照所有设置和说明here和here进行操作,但每次尝试运行测试时,我仍然会收到此错误:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug]
此异常发生在我打电话setupActivity()
的测试的第一行。
答案 0 :(得分:0)
当使用自定义RobolectricTestRunner并提供缺少的系统属性时,此插件适用于我。
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);
}
}
编辑: 您还必须在单元测试运行配置中使用其他工作目录。最好是更改默认配置并使用$ MODULE_DIR $ https://code.google.com/p/android/issues/detail?id=158015#c5