Robolectric:“找不到AndroidManifest.xml"和“无法找到资源ID#0x7f09001b"; [英] Robolectric: “AndroidManifest.xml not found” and "Unable to find resource ID #0x7f09001b"
问题描述
我正在用Roboletric运行一些测试,但是遇到了一个我无法解决的问题. 当我运行测试时,"AndroidManifest"出现以下错误:
I'm running some tests with Roboletric, but I came across a issue that I can't solve. When I run the test, the following error appears with the "AndroidManifest":
警告:在.\ AndroidManifest.xml中找不到清单文件.
WARNING: No manifest file found at .\AndroidManifest.xml.
仅退回到Android OS资源.要删除此警告,请添加注释 使用@Config(manifest = Config.NONE)的测试类.
Falling back to the Android OS resources only. To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
没有此类清单文件:.\ AndroidManifest.xml
No such manifest file: .\AndroidManifest.xml
我尝试了以下失败失败的解决方案:
I've tried these solutions that failed:
@Config (manifest = Config.DEFAULT_MANIFEST_NAME)
@Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 26)
@Config( constants = BuildConfig.class, manifest="src/main/AndroidManifest.xml", sdk = 26 )
执行过程中的另一个错误是:
And the other error during execution is:
android.content.res.Resources $ NotFoundException:无法找到 软件包[android,org.robolectric.default]中的资源ID#0x7f09001b
android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f09001b in packages [android, org.robolectric.default]
...
在
com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:52)
com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:52)
出现错误的这一行是以下代码:
This line that have the error is the following code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
具体位于:setContentView(R.layout.activity_main);
对我来说,我在这个问题上没有道理...
For me I didn't see sense in this issue ...
详细信息:
-
测试类位于以下文件夹中:
app\src\test\java\com\example\robertoassad
测试是:
@RunWith( RobolectricTestRunner.class)
public class Roboletric {
@Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.button2).performClick();
The test is:
@RunWith( RobolectricTestRunner.class)
public class Roboletric {
@Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.button2).performClick();
Intent expectedIntent = new Intent(activity, SecondActivity.class);
Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actual.getComponent());
}
}
推荐答案
我遇到了与您面临的问题类似的问题. 发布者jongerrish 在Robolectric GitHub问题上解决了该问题我.
I had a similar problem to the one you face. The post by jongerrish on the Robolectric GitHub Issue about this resolved the problem for me.
最适合我的答案是在模块的build.gradle
文件中添加testOptions
块:
The aspect of the answer that worked for me was to add a testOptions
block in my module's build.gradle
file:
testOptions {
unitTests {
includeAndroidResources = true
}
}
添加此块后,我的测试能够运行和访问String资源.
After adding this block my tests were able to run and access String resources.
这篇关于Robolectric:“找不到AndroidManifest.xml"和“无法找到资源ID#0x7f09001b";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!