我已经创建了一个新的TestProject并在我的testMethod中添加了以下行:
Robolectric.getShadowApplication().getString(R.string.mystring);
Run Code Online (Sandbox Code Playgroud)
我的测试失败了
android.content.res.Resources$NotFoundException: unknown resource 2131558482
Run Code Online (Sandbox Code Playgroud)
控制台显示以下警告:
WARNING: No manifest file found at .\..\..\MyProject\AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
WARNING: no system properties value for ro.build.date.utc
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml是否需要获取字符串资源?我尝试通过org.robolectric.Config.properties和@Config添加Manifest,但警告仍然发生,我无法获取字符串资源.我确保清单的相对路径是正确的.我也试过更改JUnit运行配置,但这没有帮助.
我刚刚用一个新应用程序设置了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);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经照着所有的设置和说明这里和这里,但我仍然得到这个错误每次我尝试运行测试时间:
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug]
Run Code Online (Sandbox Code Playgroud)
此异常发生在我正在调用的测试的第一行setupActivity().
android robolectric android-studio robolectric-gradle-plugin